Skip to content

Snippet: Add Videos To WooCommerce Products

// Add video field to Total metabox for WooCommerce products
function add_woo_to_product_settings( $array ) {

    // Create your own tab - cool!
    $array['woo_video_tab'] = array(
        'title'     => __( 'Video', 'wpex' ), // Tab title
        'post_type' => array( 'product' ), // Used to limit by post type, to display on all don't add this param
        'settings'  => array(
            'wpex_post_video' => array(
                'title'         => __( 'Video', 'wpex' ), // Custom field title
                'description'   => __( 'Product video', 'wpex' ), // Custom field description
                'id'            => 'wpex_post_video', // Custom field ID used to retrive via get_post_meta
                'type'          => 'text', // Type to display in the admin
            ),
        ),
    );

    // Return fields
    return $array;

}
add_filter( 'wpex_metabox_array', 'add_woo_to_product_settings' );

// Display product video instead of image/slider
if ( ! function_exists( 'woocommerce_show_product_images' ) ) }
	function woocommerce_show_product_images() {

	    // Get video and display
	    if ( $video = get_post_meta( get_the_ID(), 'wpex_post_video', true ) ) {

	        // Sanitize video URL
	        $video = esc_url( $video );

	        // Display video
	        echo '<div class="images"><div class="responsive-video-wrap">'. wp_oembed_get( $video ) .'</div></div>';

	    } else {

	        // No video defined so get thumbnail
	        wc_get_template( 'single-product/product-image.php' );

	    }

	}
}
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top