Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*========================================
- VIDEOS
- ========================================== */
- // Registers the new post type and taxonomy
- function wpt_video_posttype() {
- register_post_type( 'videos',
- array(
- 'labels' => array(
- 'name' => __( 'videos' ),
- 'singular_name' => __( 'video' ),
- 'add_new' => __( 'Add New video' ),
- 'add_new_item' => __( 'Add New video' ),
- 'edit_item' => __( 'Edit video' ),
- 'new_item' => __( 'Add New video' ),
- 'view_item' => __( 'View video' ),
- 'search_items' => __( 'Search video' ),
- 'not_found' => __( 'No videos found' ),
- 'not_found_in_trash' => __( 'No videos found in trash' )
- ),
- 'public' => true,
- 'supports' => array( 'title', 'thumbnail' ),
- 'capability_type' => 'post',
- 'rewrite' => array("slug" => "videos"), // Permalinks format
- 'menu_position' => 5,
- 'register_meta_box_cb' => 'add_videos_metaboxes'
- )
- );
- }
- add_action( 'init', 'wpt_video_posttype' );
- add_action( 'add_meta_boxes', 'add_videos_metaboxes' );
- // Add the videos Meta Boxes
- function add_videos_metaboxes() {
- add_meta_box('wpt_videos_details', 'video details', 'wpt_videos_details', 'videos', 'normal', 'default');
- }
- // The video videoid Metabox
- function wpt_videos_details() {
- global $post;
- // Noncename needed to verify where the data originated
- echo '<input type="hidden" name="videometa_noncename" id="videometa_noncename" value="' .
- wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
- // Get the videoid data if its already been entered
- $videoid = get_post_meta($post->ID, '_videoid', true);
- $videotype = get_post_meta($post->ID, '_videotype', true);
- // Echo out the field
- echo '<p><label>Video ID:</label> <input type="text" name="_videoid" value="' . $videoid . '" size="30" /></p>';
- echo '<p><label>Video Type: </label>';
- echo '<select id="videotype" name="videotype">';
- echo '<option value="">Select</option>';
- echo '<option value="Youtube">Youtube</option>';
- echo '<option value="Vimeo">Vimeo</option>';
- echo '</select>';
- echo '<script>jQuery(document).ready(function(){ jQuery("#videotype").val(' . $videotype. ') });</script></div>';
- }
- // Save the Metabox Data
- function wpt_save_videos_meta($post_id, $post) {
- // verify this came from the our screen and with proper authorization,
- // because save_post can be triggered at other times
- if ( !wp_verify_nonce( $_POST['videometa_noncename'], plugin_basename(__FILE__) )) {
- return $post->ID;
- }
- // Is the user allowed to edit the post or page?
- if ( !current_user_can( 'edit_post', $post->ID ))
- return $post->ID;
- // OK, we're authenticated: we need to find and save the data
- // We'll put it into an array to make it easier to loop though.
- $videos_meta['_videoid'] = $_POST['_videoid'];
- $videos_meta['_videotype'] = $_POST['_videotype'];
- // Add values of $videos_meta as custom fields
- foreach ($videos_meta as $key => $value) { // Cycle through the $videos_meta array!
- if( $post->post_type == 'revision' ) return; // Don't store custom data twice
- $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
- if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
- update_post_meta($post->ID, $key, $value);
- } else { // If the custom field doesn't have a value
- add_post_meta($post->ID, $key, $value);
- }
- if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
- }
- }
- add_action('save_post', 'wpt_save_videos_meta', 1, 2); // save the custom fields
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement