This first snippet just declares the box and where it should appear
function my_new_box() {
$screens = array( 'post', 'page' ); // this is the post type that the box will appear on (post,page,custom post type) separated by commas
foreach ( $screens as $screen ) { // 1122AM
add_meta_box(
'my_new_box', // the id of the div that controls the meta box. Case you want to style it
__( 'My New Box', 'studio2108.com' ), // little title that appears in the meta box in the editor
'my_new_box_content', // this is the function that prints the HTML out
$screen, // this is the post type that the box will appear on (post,page,custom post type)
'normal', // puts the box under the editor // sometimes I use "side" for side boxes
'high' // places it first under the editor
);
} // 1122AM
}
add_action( 'add_meta_boxes', 'my_new_box' );
This second snippet puts the fields in the box
function my_new_box_content( $post ) { //926AM
wp_nonce_field( plugin_basename( __FILE__ ), 'my_new_box_nonce' ); // sets my hidden NONCE field data
$this_post_id= $post->ID;
$new_box_text_field = get_post_meta( $this_post_id, 'new_box_text_field', true );
$new_box_textarea = get_post_meta( $this_post_id, 'new_box_textarea', true );
$new_box_select = get_post_meta( $this_post_id, 'new_box_select', true );
$new_box_checkbox = get_post_meta( $this_post_id, 'new_box_checkbox', true );
?>
<label>new box text field</label>
<input type="text" name="new_box_text_field" class="new_box_text_field" value="<? echo stripslashes($new_box_text_field); ?>">
<label>new_box_textarea</label>
<textarea name="new_box_textarea"><? echo stripslashes(echo $new_box_textarea); ?></textarea>
<label>new_box_select</label>
<select name="new_box_select">
<option value="<?php echo stripslashes($new_box_select); ?>"><?php echo stripslashes($new_box_select); ?></option>
<option value=""></option>
<option value="something_1">something_1</option>
<option value="something_1">something_1</option>
</select>
<label>new_box_checkbox</label>
<input type="checkbox" name="new_box_checkbox" value="yes">
<?
} //926AM
This last part is the “catcher” of the input from your added box. It basically saves what you enter in the meta box.
function my_new_box_save( $post_id ) { // 847AM
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // this stops auto save from updating my fields automatically
return;
if ( !wp_verify_nonce( $_POST['my_new_box_nonce'], plugin_basename( __FILE__ ) ) ) // checks the nonce on the save
return;
update_post_meta( $post_id, 'new_box_text_field', $_POST['new_box_text_field'] );
update_post_meta( $post_id, 'new_box_textarea', $_POST['new_box_textarea'] );
update_post_meta( $post_id, 'new_box_select', $_POST['new_box_select'] );
update_post_meta( $post_id, 'new_box_checkbox', $_POST['new_box_checkbox'] );
} // 847AM
add_action( 'save_post', 'my_new_box_save' );