Advertisement
Guest User

Metabox

a guest
Oct 23rd, 2011
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. /* Custom Meta Box */
  2.  
  3. add_action( 'add_meta_boxes', 'cd_meta_box_add' );
  4. function cd_meta_box_add()
  5. {
  6.     add_meta_box( 'my-meta-box-id', 'Music', 'cd_meta_box_cb', 'page', 'side', 'low' );
  7. }
  8.  
  9. function cd_meta_box_cb( $post )
  10. {
  11.     $values = get_post_custom( $post->ID );
  12.     $text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
  13.     $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : '';
  14.     $check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : '';
  15.     wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
  16.     ?>
  17.    
  18.     <p>
  19.         <select name="my_meta_box_select" id="my_meta_box_select">
  20.             <option value="none" <?php selected( $selected, 'none' ); ?>>None</option>
  21.             <?php $audios =& get_children( 'post_type=attachment&post_mime_type=audio/mpeg' );
  22.                 foreach ( (array) $audios as $attachment_id => $attachment ) { ?>
  23.             <option value="<?php echo $attachment_id; ?>" <?php selected( $selected, $attachment_id ); ?>><?php echo $attachment->post_title; ?></option>
  24.         <?php } ?>
  25.         </select>
  26.     </p>
  27.     <?php  
  28. }
  29.  
  30. add_action( 'save_post', 'cd_meta_box_save' );
  31. function cd_meta_box_save( $post_id )
  32. {
  33.     // Bail if we're doing an auto save
  34.     if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  35.    
  36.     // if our nonce isn't there, or we can't verify it, bail
  37.     if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
  38.    
  39.     // if our current user can't edit this post, bail
  40.     if( !current_user_can( 'edit_post' ) ) return;
  41.    
  42.     // now we can actually save the data
  43.     $allowed = array(
  44.         'a' => array( // on allow a tags
  45.             'href' => array() // and those anchords can only have href attribute
  46.         )
  47.     );
  48.    
  49.     // Probably a good idea to make sure your data is set
  50.     if( isset( $_POST['my_meta_box_text'] ) )
  51.         update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
  52.        
  53.     if( isset( $_POST['my_meta_box_select'] ) )
  54.         update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
  55.        
  56.     // This is purely my personal preference for saving checkboxes
  57.     $chk = ( isset( $_POST['my_meta_box_check'] ) && $_POST['my_meta_box_check'] ) ? 'on' : 'off';
  58.     update_post_meta( $post_id, 'my_meta_box_check', $chk );
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement