How To Automatically Create Custom Field When A Post/Page is Created

Hi Friends, here is a useful tip for you… Some times we may need to use lots of custom fields in our wordpress page or post. It will be too difficult to copy and paste each of these custom fields from one post to another. So I found a small bit of code that will automatically create custom field when a post or page published.Here it is..

add_action(‘publish_page’, ‘automatically_add_custom_field’);
add_action(‘publish_post’, ‘automatically_add_custom_field’);
function automatically_add_custom_field($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, ‘custom_field-name1’, ‘custom field value’, true);
add_post_meta($post_ID, ‘custom_field-name2’, ‘custom field value’, true); }
}

Change red color text to your custom field name and value…
So,by using this code you can create any number of custom fields according to your need.

7 thoughts on “How To Automatically Create Custom Field When A Post/Page is Created

  1. webmeester.eu

    I am really thankful to you for proving us with the code that will automatically create custom field when a post or page published.The visit was useful.I will visit you again for something useful like this one.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *