Skip to content

Instantly share code, notes, and snippets.

@vyatri
Last active December 14, 2015 08:08
Show Gist options
  • Save vyatri/5055326 to your computer and use it in GitHub Desktop.
Save vyatri/5055326 to your computer and use it in GitHub Desktop.
Simple video post type script for wordpress. created with Custom Post type UI plugin and additional modified script from http://wp.tutsplus.com/tutorials/plugins/how-to-create-custom-wordpress-writemeta-boxes/ and http://wp.tutsplus.com/tutorials/creative-coding/youtube-and-vimeo-video-gallery-with-wordpress/
<?php
register_post_type('video', array( 'label' => 'Video','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => true,'rewrite' => array('slug' => 'video'),'query_var' => true,'has_archive' => true,'exclude_from_search' => false,'supports' => array('title','custom-fields','author',),'labels' => array (
'name' => 'Video',
'singular_name' => 'Video',
'menu_name' => 'Video',
'add_new' => 'Add Video',
'add_new_item' => 'Add New Video',
'edit' => 'Edit',
'edit_item' => 'Edit Video',
'new_item' => 'New Video',
'view' => 'View Video',
'view_item' => 'View Video',
'search_items' => 'Search Video',
'not_found' => 'No Video Found',
'not_found_in_trash' => 'No Video Found in Trash',
'parent' => 'Parent Video',
),) );
add_action( 'add_meta_boxes', 'my_meta_box_add' );
function my_meta_box_add()
{
add_meta_box( 'my_video_meta', 'Video detail', 'my_meta_box_cb', 'video', 'normal', 'high' );
}
function my_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_video_meta_text'] ) ? esc_attr( $values['my_video_meta_text'][0] ) : '';
$selected = isset( $values['my_video_meta_select'] ) ? esc_attr( $values['my_video_meta_select'][0] ) : '';
$textarea = isset( $values['my_video_meta_textarea'] ) ? $values['my_video_meta_textarea'][0] : '';
wp_nonce_field( 'my_video_meta_nonce', 'meta_box_nonce' );
?>
<table width="100%">
<tr>
<td width="100">
<label for="my_video_meta_text">Video ID</label>
</td>
<td>
<input type="text" name="my_video_meta_text" id="my_video_meta_text" value="<?php echo $text; ?>" style="width:50%" />
</td>
</tr>
<tr>
<td>
<label for="my_video_meta_select">Video site</label>
</td>
<td>
<select name="my_video_meta_select" id="my_video_meta_select">
<option value="youtube" <?php selected( $selected, 'red' ); ?>>Youtube</option>
<option value="vimeo" <?php selected( $selected, 'blue' ); ?>>Vimeo</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="my_video_meta_textarea">Deskripsi video</label>
</td>
<td>
<textarea name="my_video_meta_textarea" id="my_video_meta_textarea" style="width:100%" rows="10"><?php echo $textarea; ?></textarea>
</td>
</tr>
</table>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_video_meta_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Probably a good idea to make sure your data is set
if( isset( $_POST['my_video_meta_text'] ) )
update_post_meta( $post_id, 'my_video_meta_text', wp_kses( $_POST['my_video_meta_text'], $allowed ) );
if( isset( $_POST['my_video_meta_select'] ) )
update_post_meta( $post_id, 'my_video_meta_select', esc_attr( $_POST['my_video_meta_select'] ) );
if( isset( $_POST['my_video_meta_textarea'] ) )
update_post_meta( $post_id, 'my_video_meta_textarea', $_POST['my_video_meta_textarea'] );
}
<?php get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<div>
<?php
$videosite = get_post_meta($post->ID, 'my_video_meta_select', single);
$videoid = get_post_meta($post->ID, 'my_video_meta_text', single);
$description = get_post_meta($post->ID, 'my_video_meta_textarea', single);
if ($videosite == 'vimeo')
{
echo '<iframe src="http://player.vimeo.com/video/'.$videoid.'" width="675" height="428" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}
else if ($videosite == 'youtube')
{
echo '<iframe width="675" height="428" src="http://www.youtube.com/embed/'.$videoid.'" frameborder="0" allowfullscreen></iframe>';
}
else
{
echo 'Please select a Video Site via the WordPress Admin';
}
?>
</div>
<p><?php echo wpautop( wptexturize($description) ); ?></p>
<?php endwhile; // end of the loop. ?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment