Advertisement
luisabarca

Multi fields custom metabox

Oct 20th, 2011
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. add_action('add_meta_boxes', 'mycpt_meta_boxes');
  2.  
  3. function mycpt_meta_boxes()
  4. {
  5.     add_meta_box( 'cpt_metabox-1', __( 'Extra fields' ), 'mycpt_meta_box_content', 'post', 'normal', 'high');
  6. }
  7.  
  8. // }}}
  9. // {{{
  10.  
  11. function mycpt_meta_box_content()
  12. {
  13.     global $post;  
  14.  
  15.     include 'form.php';
  16.     wp_nonce_field('mycpt_save', 'mycpt_nonce');
  17. }
  18.  
  19. add_action('save_post', 'mycpt_save_postdata', 10, 2);
  20.  
  21. function mycpt_save_postdata( $post_id )
  22. {
  23.     global $wpdb;
  24.  
  25.     if ( !wp_verify_nonce( $_POST['mycpt_nonce'], 'mycpt_save' ) ) {
  26.         return $post_id;
  27.     }
  28.  
  29.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  30.         return $post_id;
  31.     }
  32.  
  33.     // Check permissions ....
  34.  
  35.     /*
  36.      * Videos names/titles
  37.      */
  38.     $videos_titulos = $_POST['video_title'];
  39.    
  40.     // First is empty
  41.     array_shift($videos_titulos);
  42.  
  43.     /*
  44.      * Video URLs
  45.      */
  46.     $videos_urls  = $_POST['video_url'];
  47.     // first is empty
  48.     array_shift($videos_urls);
  49.    
  50.     // How many ?
  51.     $videos_count = sizeof($videos_titulos);
  52.     $videos = array();
  53.     $content = '';
  54.  
  55.     for ($i = 0; $i <= $videos_count; $i++) {
  56.         $title = $videos_titulos[$i];
  57.         $url    = $videos_urls[$i];
  58.  
  59.         // check if empty
  60.         if (!empty($title) && !empty($url)) {
  61.             $videos[] = array(
  62.                 'title' => $title,
  63.                 'url'   => $url,
  64.                 'image' => get_youtube_video_thumb($url)
  65.             );
  66.         }
  67.     }
  68.  
  69.     // Serializamos videos en caso que existan, si no, eliminar el post meta
  70.     if ( sizeof($videos) > 0 ) {
  71.         $content = serialize($videos);
  72.     }
  73.  
  74.  
  75.     update_post_meta($post_id, 'videos', $content);
  76. }
  77.  
  78. /**
  79.  *
  80.  *
  81.  */
  82. function get_youtube_video_thumb($video_url)
  83. {
  84.     $thumb = '';
  85.     $myurl = parse_url($video_url);
  86.    
  87.     parse_str($myurl['query'], $myquery);
  88.    
  89.     $video = trim($myquery['v']);
  90.    
  91.     if ( !empty($video) && strlen($video) > 3 ) {
  92.         $thumb = 'http://img.youtube.com/vi/' . $video . '/default.jpg';
  93.     }
  94.    
  95.     return $thumb;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement