Skip to content

Instantly share code, notes, and snippets.

@Kenshino
Last active August 29, 2015 14:18
Show Gist options
  • Save Kenshino/81136b4e179359dec328 to your computer and use it in GitHub Desktop.
Save Kenshino/81136b4e179359dec328 to your computer and use it in GitHub Desktop.
Video replacement for Featured Image
<?php
add_action( 'woocommerce_product_options_general_product_data', 'catalog_add_video_field' );
add_action( 'woocommerce_process_product_meta', 'catalog_save_video_url' , 10, 2 );
add_filter( 'woocommerce_single_product_image_html', 'catalog_set_featured_video' , 20 );
function catalog_add_video_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_video_url',
'label' => __( 'Featured Video URL', 'catalog' ),
'placeholder' => __( 'Video URL', 'catalog' ),
'description' => __( 'Enter the URL for the video you want to show in place of the featured image in the product detail page', 'catalog' ),
) );
echo '</div>';
}
function catalog_save_video_url( $post_id, $post ) {
if ( isset( $_POST['_video_url'] ) )
update_post_meta( $post_id, '_video_url', esc_url( $_POST['_video_url'] ) );
}
function get_video_url() {
global $post;
$post_id = isset( $post->ID ) ? $post->ID : 0;
$video_url = get_post_meta( $post_id, '_video_url', true );
$video_url = empty( $video_url ) ? false : $video_url;
return $video_url;
}
function catalog_set_featured_video( $html ) {
global $post, $woocommerce;
if( ! get_video_url() ) return $html;
if ( function_exists( 'wc_get_image_size' ) ) {
$size = wc_get_image_size( apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
} else {
$size = $woocommerce->get_image_size( apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
}
$video_width = $size['width'];
$video_height = $size['height'];
$supported = wp_get_video_extensions();
$video_url = get_video_url();
$video_pathinfo = pathinfo($video_url);
$video_ext = $video_pathinfo['extension'];
if ( in_array($video_ext, $supported) ) {
echo do_shortcode('[video src="' . $video_url . '" width="' . $video_width . '" height="' . $video_height . '"]');
}
else {
echo wp_oembed_get($video_url, array('width' => $video_width, 'height' => $video_height));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment