Advertisement
keihead

Videos CPT3

Jul 2nd, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.53 KB | None | 0 0
  1. /*========================================
  2.                    VIDEOS
  3. ========================================== */
  4.  
  5. // Registers the new post type and taxonomy
  6.  
  7. function wpt_video_posttype() {
  8.     register_post_type( 'videos',
  9.         array(
  10.             'labels' => array(
  11.                 'name' => __( 'videos' ),
  12.                 'singular_name' => __( 'video' ),
  13.                 'add_new' => __( 'Add New video' ),
  14.                 'add_new_item' => __( 'Add New video' ),
  15.                 'edit_item' => __( 'Edit video' ),
  16.                 'new_item' => __( 'Add New video' ),
  17.                 'view_item' => __( 'View video' ),
  18.                 'search_items' => __( 'Search video' ),
  19.                 'not_found' => __( 'No videos found' ),
  20.                 'not_found_in_trash' => __( 'No videos found in trash' )
  21.             ),
  22.             'public' => true,
  23.             'supports' => array( 'title', 'thumbnail'  ),
  24.             'capability_type' => 'post',
  25.             'rewrite' => array("slug" => "videos"), // Permalinks format
  26.             'menu_position' => 5,
  27.             'register_meta_box_cb' => 'add_videos_metaboxes'
  28.         )
  29.     );
  30. }
  31.  
  32. add_action( 'init', 'wpt_video_posttype' );
  33. add_action( 'add_meta_boxes', 'add_videos_metaboxes' );
  34. // Add the videos Meta Boxes
  35.  
  36. function add_videos_metaboxes() {
  37.     add_meta_box('wpt_videos_details', 'video details', 'wpt_videos_details', 'videos', 'normal', 'default');
  38. }
  39. // The video videoid Metabox
  40.  
  41. function wpt_videos_details() {
  42.     global $post;
  43.    
  44.     // Noncename needed to verify where the data originated
  45.     echo '<input type="hidden" name="videometa_noncename" id="videometa_noncename" value="' .
  46.     wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  47.    
  48.     // Get the videoid data if its already been entered
  49.     $videoid = get_post_meta($post->ID, '_videoid', true);
  50.     $videotype = get_post_meta($post->ID, '_videotype', true);
  51.     // Echo out the field
  52.     echo '<p><label>Video ID:</label> <input type="text" name="_videoid" value="' . $videoid  . '" size="30" /></p>';
  53.     echo '<p><label>Video Type: </label>';
  54.     echo '<select id="videotype" name="videotype">';
  55.     echo '<option value="">Select</option>';
  56.     echo '<option value="Youtube">Youtube</option>';
  57.     echo '<option value="Vimeo">Vimeo</option>';
  58.     echo '</select>';
  59.     echo '<script>jQuery(document).ready(function(){ jQuery("#videotype").val(' . $videotype. ') });</script></div>';
  60.  
  61. }
  62. // Save the Metabox Data
  63.  
  64. function wpt_save_videos_meta($post_id, $post) {
  65.    
  66.     // verify this came from the our screen and with proper authorization,
  67.     // because save_post can be triggered at other times
  68.     if ( !wp_verify_nonce( $_POST['videometa_noncename'], plugin_basename(__FILE__) )) {
  69.     return $post->ID;
  70.     }
  71.  
  72.     // Is the user allowed to edit the post or page?
  73.     if ( !current_user_can( 'edit_post', $post->ID ))
  74.         return $post->ID;
  75.  
  76.     // OK, we're authenticated: we need to find and save the data
  77.     // We'll put it into an array to make it easier to loop though.
  78.    
  79.     $videos_meta['_videoid'] = $_POST['_videoid'];
  80.     $videos_meta['_videotype'] = $_POST['_videotype'];
  81.     // Add values of $videos_meta as custom fields
  82.    
  83.     foreach ($videos_meta as $key => $value) { // Cycle through the $videos_meta array!
  84.         if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  85.         $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  86.         if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  87.             update_post_meta($post->ID, $key, $value);
  88.         } else { // If the custom field doesn't have a value
  89.             add_post_meta($post->ID, $key, $value);
  90.         }
  91.         if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  92.     }
  93.  
  94. }
  95.  
  96. add_action('save_post', 'wpt_save_videos_meta', 1, 2); // save the custom fields
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement