Custom Meta Box

/* Use the admin_menu action to define the custom boxes */
add_action(‘admin_menu’, ‘nyc_boroughs_add_custom_box’);
/* Adds a custom section to the “side” of the post edit screen */
function nyc_boroughs_add_custom_box() {
add_meta_box(‘nyc_boroughs’, ‘Applicable Borough’, ‘nyc_boroughs_custom_box’, ‘post’, ‘side’, ‘high’);
}
/* prints the custom field in the new custom post section */
function nyc_boroughs_custom_box() {
//get post meta value
global $post;
$custom = get_post_meta($post->ID,’_nyc_borough’,true);
// use nonce for verification
echo ‘<input type=”hidden” name=”nyc_boroughs_noncename” id=”nyc_boroughs_noncename” value=”‘.wp_create_nonce(‘nyc-boroughs’).'” />’;
// The actual fields for data entry
echo ‘<label for=”nyc_borough”>Borough</label>’;
echo ‘<select name=”nyc_borough” id=”nyc_borough” size=”1″>’;
//lets create an array of boroughs to loop through
$boroughs = array(‘Manhattan’,’Brooklyn’,’Queens’,’The Bronx’,’Staten Island’);
foreach ($boroughs as $borough) {
echo ‘<option value=”‘.$borough.'”‘;
if ($custom == $borough) echo ‘ selected=”selected”‘;
echo ‘>’.$borough.'</option>’;
}
echo “</select>”;
}
/* use save_post action to handle data entered */
add_action(‘save_post’, ‘nyc_boroughs_save_postdata’);
/* when the post is saved, save the custom data */
function nyc_boroughs_save_postdata($post_id) {
// verify this with nonce because save_post can be triggered at other times
if (!wp_verify_nonce($_POST[‘nyc_boroughs_noncename’], ‘nyc-boroughs’)) return $post_id;
// do not save if this is an auto save routine
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) return $post_id;
$nyc_borough = $_POST[‘nyc_borough’];
update_post_meta($post_id, ‘_nyc_borough’, $nyc_borough);
}


Custom Meta Box