I am trying to show some variables when I hover over products image. For that I am using a shortcode to show the variable I need and the function do_shortcode(). However the needed variable is not showing when i hover over the product, while when I post the shortcode in product description for example it gives the expected results. In this code you can see the variable I need $theHeight defined as global but is empty when called with shortcode in php snipet (using WP code snipet).Does somebody know how to fix this. I am actualy really new to php and and wordpress development.

add_action( 'woocommerce_before_add_to_cart_form', 'get_iptc_info', 10 ); 
function get_iptc_info(){
    global $product;
    global $theHeight;
    $image_id = $product-> get_image_id();
    $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    $exif = exif_read_data($image_url, 'IFD0');
    echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";
    $exif = exif_read_data($image_url, 0, true);
    echo $image_url."<br />\n";
    foreach ($exif as $key => $section) {
        foreach ($section as $name => $val) {
            //echo "$key.$name: $val<br />\n";
            if($key.$name==="COMPUTED"."Height") {
                echo "$name: $val<br />\n";
                $theHeight =$val;

and this the shortcode

add_shortcode( 'my_show_variable_shortcode', 'iptc_shortcode' );
function iptc_shortcode() {
    global $theHeight;
    return $theHeight;

and finaly this is the code to show the variable on product image hover

add_action( 'woocommerce_before_shop_loop_item', 'wc_add_customdesc' );
    global $theHeight;
function wc_add_customdesc() {
    global $product, $theHeight;
    $creationDate = get_field( 'picture_date', $product->get_id() ); 
    $authorName = get_field( 'author', $product->get_id() ); 
    $title = $product->get_title(); 
    $price = $product->get_price();
    $myDescript = $product->post-> post_excerpt;
    <div class="descShow" itemprop="description">
        <?php echo '<p class="thedataclass">' .'The shooting description is '. $myDescript . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The shooting date is '. $creationDate . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The auhtor is '. $authorName . '</p>'; ?>
        <?php echo '<p class="thedataclass">' .'The title is '. $title . '</p>'; ?>
        <?php echo do_shortcode('[my_show_variable_shortcode]');?>
        margin:10px 5px;
        max-height: 0px;
        transition: max-height 0.5s ease-out;
        overflow: hidden;
        position: absolute;
        background-color: rgba(65,65,65,0.6);
    li.product:hover .descShow{
        max-height: 100%;
        max-width: 100%;
        transition: all 0.5s ease-in-out;
    @media only screen and (max-width: 922px) {
        li.product:hover .descShow{
    @media only screen and (max-width: 545px){
        li.product:hover .descShow{


  • 2
    You've misunderstood how global variables work in PHP. Those values are not persistent across requests. If you set the variable in one place on one page, it will not exist when the user navigates to a different page. In your case you're setting a value on a product page, and then trying to show it on the shop page/category page. That won't work. Commented Oct 7, 2023 at 13:58
  • Ok thanks Jacob so I guess I need to save in the database and then retrieve data?
    – Mehdi
    Commented Oct 7, 2023 at 14:45
  • or maybe should I try to do add_action on same page might be easier
    – Mehdi
    Commented Oct 7, 2023 at 15:55
  • Jacob thanks a lot I tried as you explained and it worked fine, just changed both add_action to woocommerce_before_shop_loop_item. thank you. Now I better understand what global is
    – Mehdi
    Commented Oct 7, 2023 at 18:36


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.