Advertisement
miriamdepaula

WordPress: Simple metabox script

Jul 3rd, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.72 KB | None | 0 0
  1. <?php
  2. /* A simple way to create metaboxes in WordPress */
  3.  
  4. add_action( 'add_meta_boxes', 'video_meta_box' );
  5. function video_meta_box()
  6. {
  7.     /*
  8.     http://codex.wordpress.org/Function_Reference/add_meta_box
  9.     add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
  10.     */
  11.     add_meta_box( 'video_meta_box', 'Video', 'video_meta_box_setup', 'post', 'normal', 'high' );
  12. }
  13.  
  14. function video_meta_box_setup()
  15. {
  16.     global $post;
  17.    
  18.     /* http://codex.wordpress.org/Function_Reference/wp_nonce_field */ 
  19.     wp_nonce_field( plugin_basename( __FILE__ ), 'video_meta_box_nonce_name' );
  20.    
  21.     /* we take the data stored separately here... */
  22.     $video_id = get_post_meta( $post->ID, '_video_id', true);
  23.     $video_image = get_post_meta( $post->ID, '_video_image', true);
  24.  
  25.     /*
  26.     If you have stored all data in one array.....
  27.     $video_data = get_post_meta( $post->ID, '_video_data', true);
  28.     $video_id = $video_data['video_id'];
  29.     $video_image = $video_data['video_image'];
  30.     */ 
  31. ?> 
  32.     <table class="form-table">
  33.        
  34.         <tr>
  35.             <td><label for="video_id">Informe o ID do vídeo</label></td>
  36.             <td>
  37.                 <input class="regular-text" type="text" id="video_id" name="video[video_id]" value="<?php echo $video_id?>" />
  38.             </td>        
  39.         </tr>
  40.        
  41.         <tr>
  42.             <td><label for="projeto">Informe a URL da imagem do vídeo (thumbnail)</label></td>
  43.             <td>                
  44.                 <input class="regular-text" type="text" id="video_image" name="video[video_image]" value="<?php echo $video_image?>" />
  45.            </td>
  46.         </tr>        
  47.    
  48.     </table>
  49.        
  50. <?php }
  51.  
  52. add_action( 'save_post', 'video_meta_box_save' );
  53. function video_meta_box_save( $post_id ) {
  54.    
  55.       // verify if this is an auto save routine.
  56.       // If it is our form has not been submitted, so we dont want to do anything
  57.       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  58.       return;
  59.      
  60.       // verify this came from the our screen and with proper authorization,
  61.       // because save_post can be triggered at other times
  62.       if ( !wp_verify_nonce( $_POST['video_meta_box_nonce_name'], plugin_basename( __FILE__ ) ) )
  63.       return;
  64.    
  65.      //check permissions
  66.      if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;
  67.    
  68.      $video = $_POST['video'];
  69.      
  70.      /* To store in separate meta fields */
  71.      update_post_meta( $post_id, '_video_id', $video['video_id'] );
  72.      update_post_meta( $post_id, '_video_image', $video['video_image'] );  
  73.  
  74.     /* To store all data into one array
  75.      update_post_meta( $post_id, '_video_data', $video );  
  76.     */
  77.  
  78.      return $post_id;  
  79. }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement