php - User Meta Value not echoing despite Var_Dump Showing correct string

I am going absolutely insane with this one, I cannot for the life of me figure out why I am not receiving the string con

I am going absolutely insane with this one, I cannot for the life of me figure out why I am not receiving the string content.

var_dump is outputting the correct result perfectly, yet the content for $havemeta is empty. Really need some input.

add_action( 'woocommerce_before_main_content', 'shopping_location_text', 10 );
    global $current_user;
    get_currentuserinfo(); // wordpress global variable to fetch logged in user info
    $userID = $current_user->ID; // logged in user's ID
    $havemeta = get_user_meta($userID, 'location_select', true); // stores the value of logged in user's meta data for 'location_select'.
    var_dump($havemeta);

        function shopping_location_text() {
          if( is_shop() ) {
            print '<p class="shopping-location-text">YOUR ARE CURRENTLY SHOPPING IN <span style="FONT-WEIGHT: 700;COLOR: #fa4516;">' . $havemeta . '</span></p>';
          }
}

I am going absolutely insane with this one, I cannot for the life of me figure out why I am not receiving the string content.

var_dump is outputting the correct result perfectly, yet the content for $havemeta is empty. Really need some input.

add_action( 'woocommerce_before_main_content', 'shopping_location_text', 10 );
    global $current_user;
    get_currentuserinfo(); // wordpress global variable to fetch logged in user info
    $userID = $current_user->ID; // logged in user's ID
    $havemeta = get_user_meta($userID, 'location_select', true); // stores the value of logged in user's meta data for 'location_select'.
    var_dump($havemeta);

        function shopping_location_text() {
          if( is_shop() ) {
            print '<p class="shopping-location-text">YOUR ARE CURRENTLY SHOPPING IN <span style="FONT-WEIGHT: 700;COLOR: #fa4516;">' . $havemeta . '</span></p>';
          }
}
Share Improve this question asked Aug 20, 2019 at 4:05 BruceBrain21BruceBrain21 152 bronze badges 2
  • Does the "YOUR ARE CURRENTLY SHOPPING" text display? – Jacob Peattie Commented Aug 20, 2019 at 4:06
  • Yes perfectly! That is what is driving me insane. – BruceBrain21 Commented Aug 20, 2019 at 4:09
Add a comment  | 

1 Answer 1

Reset to default 1

The $havemeta variable isn't defined inside your shopping_location_text() function, so it isn't available for use there.

For a quick fix (or for testing/toying) you can move your code inside the function, like this:

add_action( 'woocommerce_before_main_content', 'shopping_location_text', 10 );
function shopping_location_text() {
    global $current_user;
    get_currentuserinfo(); // wordpress global variable to fetch logged in user info
    $userID = $current_user->ID; // logged in user's ID
    $havemeta = get_user_meta($userID, 'location_select', true); // stores the value of logged in user's meta data for 'location_select'.
    //var_dump($havemeta);
    if( is_shop() ) {
        print '<p class="shopping-location-text">YOUR ARE CURRENTLY SHOPPING IN <span style="FONT-WEIGHT: 700;COLOR: #fa4516;">' . $havemeta . '</span></p>';
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231723a4617706.html

相关推荐

  • php - User Meta Value not echoing despite Var_Dump Showing correct string

    I am going absolutely insane with this one, I cannot for the life of me figure out why I am not receiving the string con

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信