How to automatically create a custom field when a post is published

Paste the code below into your functions.php file. The only thing you have to do is to edit the cutsom field name on line 6.

add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
	global $wpdb;
	if(!wp_is_post_revision($post_ID)) {
		add_post_meta($post_ID, 'field-name', 'custom value', true);
	}
}

Thanks to wpCanyon for this cool tip!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

How to automatically create a custom field when a post is published

Leave a Reply

Your email address will not be published. Required fields are marked *