Simply add the code bellow in your functions.php file inside your theme’s folder. And don’t forget to change the custom field name.
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);
}
}
That’s it. Hope this helps.
Visit the best themes marketplace with over 600 premium wordpress themes.













Could you give an example why you would use this?
@Square one – I built a custom plugin for a client that shows posts based on number of views the posts have. I used this to add the “post-views” custom field when a post is published.
I think this code was published on WpRecipes.com?
It was published on wprecipes.com few days ago, and if you take a look at that post on wprecipes you can see “Thanks to wpCanyon for this cool tip!”.
Yes it was published on wprecipes.com, not sure why do you ask, but i didn’t copy it. If you take a look at that post on wprecipes.com you can see “Thanks to wpCanyon for this cool tip!”.
Thanks for the tip. I don’t have custom field setup on my Blog.. I am really looking forward to learning about custom field btw.. thanks again.
how about automatically adding the custom fields when the theme is activated? thanks.