title

Add custom field automatically when post or page is publish

 

Add custom field automatically when post or page is publish

10 Mai 2013, Posted by antoine in

Adding this snippet to the functions.php of your wordpress theme will add a custom field to a post or page when published. Don’t forget to update the FIELD_NAME and the CUSTOM VALUE.

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);
    }
}