wordpress how to add custom field for custom posts in admin side

640

Adding  and updating custom field in wordpress very simple, Please follow below step by steps then you can able to create custom field.

  1. assume that my custom post type is ‘series’  so i want to add custom field under series posts.
  2. first we have to register the post meta by using the add_action like below

add_action(‘admin_init’, ‘register_post_assets’, 1);

function register_post_assets(){

// below method is used for register meta_box under series, using third parameter we have to create function

add_meta_box(series-info-post’, __(‘series info me’), ‘add_series_info’, ‘series’, ‘advanced’, ‘high’);

}

// below function is used for display the text box in series section for adding the extra information.

function add_series_info($post) {
$detail_info= get_post_meta($post->ID, ‘series_info’, true);
echo “<label for=’series_info’>” . __(‘Please enter series detail info&nbsp; ‘, ‘foobar’) . “</label>”;
echo “<input type=’text’ name=’series_info’ id=’series_info’ value=’$detail_info’ />”;
}

//the need to write the function for adding or updating custom field info.

//need to use hook for save and update the required info.

add_action(‘save_post’, ‘save_featured_series_meta’);

function save_featured_series_meta($post_id) {

if (isset($_REQUEST[‘series_info’])) {
update_post_meta($post_id, ‘series_info’, $_REQUEST[‘series_info’]);
} else {
delete_post_meta($post_id, ‘series_info’);
}

}

// using above function we can able to create and update post meta info..