Automatically add a custom field when a post is published on WordPress.

Advertisements

Most of the WordPress users now use highly code themes that add to the functions and looks of their blogs. And for many such functions you need a add a custom field while publishing any post. Obviously many of us dont like to do this extra thing while blogging.

So here’s a solution to how to do it automatically.

Just goto your Dashboard > Appearance > Editor and look for ‘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);
}
}

Thats it, now a custom field will be added automatically every time you publish a post on your WordPress blog.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Leave a Reply