Use CMB2 to set product hero image in Essence Pro: Set header image on product & category pages to one set in CMB2 meta box or main product image - in Essence Pro theme. https://www.damiencarbery.com/2018/09/use-cmb2-to-set-product-hero-image-in-essence-pro/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Product Header Image for Essence Pro | |
Plugin URI: https://damiencarbery.com/2018/09/use-cmb2-to-set-product-hero-image-in-essence-pro/ | |
Description: Set the header image to a product image in Essence Pro. | |
Author: Damien Carbery | |
Version: 0.6 | |
*/ | |
// When on a product page retrieve image set via CMB2 meta box or first product image. | |
// Use this short-circuit filter to make it quicker on product pages - if the 'genesis_get_image' | |
// filter was used then the 'genesis_get_image()' function would have unnecessarily called a | |
// number of attachment related functions (which involve database queries). | |
add_filter( 'genesis_pre_get_image', 'dcwd_short_circuit_product_header_image', 10, 3 ); | |
function dcwd_short_circuit_product_header_image( $false, $args, $post_obj ) { | |
// Only run when a header hero image is being sought. | |
if ( 'header-hero' == $args[ 'size' ] ) { | |
$image_url = $false; | |
// Check whether an image is set via the CMB2 meta box | |
if ( is_product() ) { | |
$image_url = get_post_meta( get_the_ID(), 'header_hero', true ); | |
// Otherwise use the main product image. | |
if ( empty( $image_url ) ) { | |
$featured_image_id = get_post_thumbnail_id( get_the_ID() ); | |
// If a product image is not set then return and allow calling function use default image. | |
// Add 0 to force $featured_image_id into a number if it can be converted (this should not | |
// be needed but even when it was a numeric string like "37" it still failed is_numeric()! | |
if ( !is_numeric( $featured_image_id + 0 ) ) { | |
return $false; | |
} | |
list( $image_url ) = wp_get_attachment_image_src( $featured_image_id, 'header-hero' ); | |
// Another check to ensure the image is available. | |
if ( empty( $image_url ) ) { | |
return $false; | |
} | |
} | |
} | |
// For the product category we check term meta for the image. | |
if ( is_product_category() ) { | |
$image_url = get_term_meta( get_queried_object_id(), 'header_hero', true ); | |
// If none set then use the default. | |
if ( empty( $image_url ) ) { | |
return $false; | |
} | |
} | |
return $image_url; | |
} | |
return $false; | |
} | |
add_action( 'cmb2_admin_init', 'cmb2_product_metaboxes' ); | |
function cmb2_product_metaboxes() { | |
/// Initiate the metabox | |
$cmb_product = new_cmb2_box( array( | |
'id' => 'ep_products', | |
'title' => 'Header hero image', | |
'object_types' => array( 'product', ), // Post type | |
'context' => 'side', // Place in sidebar | |
'priority' => 'low', // near the 'Product image' and 'Product gallery' meta boxes. | |
'show_names' => true, // Show field names on the left | |
) ); | |
$cmb_product->add_field( array( | |
'desc' => 'Upload an image or enter an URL. Suggested size: 1800 x 700.', | |
'id' => 'header_hero', | |
'type' => 'file', | |
'options' => array( | |
'url' => false, // Hide the text input for the url | |
), | |
'text' => array( | |
'add_upload_file_text' => 'Set Hero Image' // Change upload button text. Default: "Add or Upload File" | |
), | |
'query_args' => array( | |
'type' => array( 'image/gif', 'image/jpeg', 'image/png', ), // Limit to image types. | |
), | |
'preview_size' => 'small', | |
) ); | |
// Add same meta box for product categories. | |
$cmb_product_category = new_cmb2_box( array( | |
'id' => 'ep_product_cat', | |
'title' => 'Header hero image', | |
'object_types' => array( 'term', ), // Store in term meta | |
'taxonomies' => array( 'product_cat', ), | |
'show_names' => true, // Show field names on the left | |
) ); | |
$cmb_product_category->add_field( array( | |
'name' => 'Hero Image', | |
'desc' => 'Upload an image or enter an URL. Suggested size: 1800 x 700.', | |
'id' => 'header_hero', | |
'type' => 'file', | |
'options' => array( | |
'url' => false, // Hide the text input for the url | |
), | |
'text' => array( | |
'add_upload_file_text' => 'Set Hero Image' // Change upload button text. Default: "Add or Upload File" | |
), | |
'query_args' => array( | |
'type' => array( 'image/gif', 'image/jpeg', 'image/png', ), // Limit to image types. | |
), | |
'preview_size' => 'small', | |
) ); | |
} | |
// Verify that CMB2 plugin is active. | |
add_action( 'admin_notices', 'verify_cmb2_active' ); | |
function verify_cmb2_active() { | |
if ( ! defined( 'CMB2_LOADED' ) ) { | |
$plugin_data = get_plugin_data( __FILE__ ); | |
$plugin_name = $plugin_data['Name']; | |
?> | |
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div> | |
<?php | |
//error_log( 'CMB2 is not active.' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment