
Ever wanted to be able to automatically create a custom field with a value when a post (or page) is created? Then this snippet is for you.

Ever wanted to be able to automatically create a custom field with a value when a post (or page) is created? Then this snippet is for you.
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!
One Response
Great code! Any idea how I can autofill the custom post with the post thumbnail url?
Trackbacks: