Skip to content

Instantly share code, notes, and snippets.

@vc27
Created March 24, 2012 17:05
Show Gist options
  • Save vc27/2185174 to your computer and use it in GitHub Desktop.
Save vc27/2185174 to your computer and use it in GitHub Desktop.
Adds a custom metabox to specified post_types allowing for youtube and vimeo oembed to be saved as hidden postmeta. Returns visalbe video url and video
<?php
/*
Plugin Name: Oembed as Post Meta
Author: Randy Hicks
Plugin URI: http://visualcoma.com
Author URI: http://visualcoma.com
Version: 0.2
Updated: 09.29.12
Description: Adds a custom metabox to specified post_types allowing for youtube and vimeo oembed
to be saved as hidden postmeta. Returns visalbe video url and video thumbnail.
#################################################################################################### */
/**
* File Name oembed-post-meta.php
* @package WordPress
* @subpackage ParentTheme_VC
* @license GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* @version 0.2
* @updated 09.29.12
**/
#################################################################################################### */
/**
* Initiate OEmbed_Post_Meta_VC
*
* @version 0.1
* @updated 02.24.12
**/
add_action( 'init', 'init_OEmbed_Post_Meta_VC' );
function init_OEmbed_Post_Meta_VC() {
$OEmbed_Post_Meta_VC = new OEmbed_Post_Meta_VC();
} // end function init_OEmbed_Post_Meta_VC
/**
* OEmbed_Post_Meta_VC Class
*
* @version 0.2
* @updated 09.29.12
**/
class OEmbed_Post_Meta_VC {
/**
* OEmbed_Post_Meta_VC Construct
**/
function OEmbed_Post_Meta_VC() {
$this->included_post_types = array( 'post', 'page' );
// Add Custom Meta Boxes
add_action( 'add_meta_boxes', array( &$this, 'add_custom_meta_boxes' ) );
// Save Post -- need to add from parent class
add_action( 'save_post', array( &$this, 'save_post_meta' ), 10, 2 );
} // end function OEmbed_Post_Meta_VC
/**
* Add Custom Fields Meta Boxes
*
* @uses add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );
*
* @version 0.1
* @updated 02.24.12
**/
function add_custom_meta_boxes( $post ) {
// Filter post_types array to allow other post_type's to utilize this metabox
$this->included_post_types = apply_filters( "oembed-included_post_types", $this->included_post_types );
foreach ( $this->included_post_types as $post_type )
add_meta_box( 'oembed_meta_box', 'Video Embed', array( &$this, 'oembed_meta_box' ), $post_type, 'normal', 'core' );
} // end function add_custom_meta_boxes
/**
* Custom Meta Box
*
* @version 0.2
* @updated 09.29.12
**/
function oembed_meta_box( $post, $metabox ) {
$oembed_video_url = get_post_meta( $post->ID, '_video_url_oembed', true );
$oembed_video_thumbnail = get_post_meta( $post->ID, '_video_thumbnail_oembed', true );
$oembed_video_data = get_post_meta( $post->ID, '_video_data_oembed', true );
?>
<p class="description">WordPress comes equipped with a number of auto embed features. This video embed feature takes advantage of the built in oembed for YouTube and Vimeo.</p>
<table class="form-table">
<tr class="form-field">
<th scope="row" valign="top">
<label for="_video_url_oembed">Video URL</label>
</th>
<td>
<input id="_video_url_oembed" type="text" name="_video_url_oembed" value="<?php echo $oembed_video_url; ?>" />
<?php
if ( !$oembed_video_url )
echo "<p class=\"description\">Enter the video url and update the post. A shortcode will be made available and the video will be added attached to this post.</p>";
?>
</td>
</tr>
<?php
if ( $oembed_video_url ) {
?>
<tr class="form-field">
<th scope="row" valign="top">
<label>Video Shortcode</label>
</th>
<td>
<input id="oembed_video_url" name="oembed_video_url" type="text" disabled="disabled" value='[embed]<?php echo $oembed_video_url; ?>[/embed]' />
<p><input style="width:100px;cursor:pointer" type="button" name="send-to-editor" value="Send to editor" /></p>
<p class="description">You may add a specific width and height to the embed code after it has been sent to the editor.<br />e.g. [embed width="123" height="456"]<?php echo $oembed_video_url; ?>[/embed]</p>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('input[name="send-to-editor"]').click(function() {
send_to_editor( $('input[name="oembed_video_url"]').val() );
});
});
</script>
</td>
</tr>
<?php
} // end if ( $oembed_video_url )
if ( $oembed_video_thumbnail ) {
?>
<tr class="form-field">
<th scope="row" valign="top">
<label>Video Thumbnail</label>
</th>
<td>
<p><img style="max-width:100%;height:auto;" src="<?php echo $oembed_video_thumbnail; ?>" alt="" /></p>
<p class="description">This image is pulled directly from video data associated with the given url. If you would like to update this image you must <a href="<?php echo $oembed_video_url; ?>" target="_blank">manage it through the associated video platform</a> then return here and update this page.</p>
</td>
</tr>
<?php
} // end if ( $oembed_video_thumbnail )
?>
</table>
<?php
echo "<input type=\"hidden\" name=\"$post->post_type-nonce\" value=\"" . wp_create_nonce( "$post->post_type-nonce" ) . "\" />";
} // end function vc_custom_page_meta
/**
* Sanitize Post Meta
*
* @version 0.2
* @updated 09.29.12
**/
function sanitize_post_meta( $new_instance, $post ) {
if ( !empty( $new_instance['_video_url_oembed'] ) ) {
$instance = $this->embed_oembed_html( $new_instance['_video_url_oembed'], $post->ID );
$instance['_video_url_oembed'] = $new_instance['_video_url_oembed'];
} else {
$instance['_video_url_oembed'] = false;
}
return $instance;
} // end function sanitize_post_meta
/**
* Update post_meta on save_post
*
* @version 0.1
* @updated 02.18.12
**/
function save_post_meta( $post_id, $post ) {
// Varify nonce
if ( !wp_verify_nonce( $_POST["$post->post_type-nonce"], "$post->post_type-nonce" ) )
return $post_id;
// Return if doing autosave
if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE )
return $post_id;
// Restrict User
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// New
$new_instance = $this->sanitize_post_meta( $_POST, $post );
foreach ( $new_instance as $key => $val ) {
$old = get_post_meta( $post_id, $key, true );
if ( !empty( $val ) )
update_post_meta( $post_id, $key, $val, $old );
elseif ( empty( $val ) )
delete_post_meta( $post_id, $key, $val);
} // end foreach ( $new as $key => $val )
} // end function save_post_meta
/**
* Filter embed_oembed_html
*
* @version 0.2
* @updated 09.29.12
*
* based on filter 'embed_oembed_html', $html, $url, $attr, $post_ID )
**/
function embed_oembed_html( $url, $post_ID ) {
if ( ! class_exists( 'WP_oEmbed' ) )
require_once( ABSPATH . WPINC . '/class-oembed.php' );
$WP_oEmbed = new WP_oEmbed();
$provider = $WP_oEmbed->discover( $url );
$data = $WP_oEmbed->fetch( $provider, $url );
if ( isset( $data ) AND $data != false ) {
$output['_video_thumbnail_oembed'] = $data->thumbnail_url;
$output['_video_data_oembed'] = $data;
return $output;
} else {
$output['_video_thumbnail_oembed'] = false;
$output['_video_data_oembed'] = false;
return $output;
}
} // end function embed_oembed_html
} // end class OEmbed_Post_Meta_VC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment