Saturday 18 January 2014

Automatically Add Custom Field When Post Is Published

There are many plugins , hacks and filters to add custom fields automatically, but i found out a better way to add custom field automatically. In order to do this you have add the snippet in your functions.php file.


ADD CUSTOM FIELD AUTOMATICALLY WHEN A POST IS PLUBLISHED.

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);
    }
} 
  
Now change the filed name and value in the snippet.

No comments:

Post a Comment