• Resolved Rajon Ahmed

    (@engrrajonahmed)


    Hi there,

    I created a shortcode with the help of OpenAI to display the avatar(custom user meta) of author. The shortcode is working but not as expected when ESI is enabled. It always returns with the fallback image instead of the author avatar. However, the shortcode works fine when I use it without ESI.

    For example:

    Working as expected: [current_author_avatar]
    Not working as expected: [esi current_author_avatar ttl=”60″]

    How to fix this issue?

    Code I am using:

    function current_author_avatar_shortcode() {
        if ( is_author() ) {
            $user_id = get_queried_object_id(); // Gets the author ID for author archives
        } else {
            $user_id = get_the_author_meta('ID'); // Gets the author ID for single post pages
        }
        
        $user = get_user_by( 'id', $user_id );
        $first_name = $user->first_name;
        $last_name = $user->last_name;
        $avatar_url = get_user_meta($user_id, 'user_avatar', true);
        
        if (empty($avatar_url)) {
            $avatar_url = 'https://static.domain.net/data/static-media/system-default/avatar.png';
        }
        
        $output = '<div class="user-avatar-con"><img class="user-avatar" src="' . $avatar_url . '" alt="' . $first_name . ' ' . $last_name . '"></div>';
        return $output;
    }
    add_shortcode('current_author_avatar', 'current_author_avatar_shortcode');
    • This topic was modified 1 year, 2 months ago by Rajon Ahmed.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support qtwrk

    (@qtwrk)

    please try extract the variable $user and $avatar_url, see what exactly are their value

    Thread Starter Rajon Ahmed

    (@engrrajonahmed)

    Hi there,

    I did what you asked. And the result is, when I use the shortcode without ESI, I get the user(author) info extracted correctly in my debug.log file. But when I use ESI in shortcode I see:

    [19-Sep-2023 05:05:13 UTC] PHP Warning:  Attempt to read property "first_name" on bool in /home/u326811434/domains/domain-x.com/public_html/academy/wp-content/plugins/code-snippets-pro/php/snippet-ops.php(511) : eval()'d code on line 9
    [19-Sep-2023 05:05:13 UTC] PHP Warning:  Attempt to read property "last_name" on bool in /home/u326811434/domains/domain-x.com/public_html/academy/wp-content/plugins/code-snippets-pro/php/snippet-ops.php(511) : eval()'d code on line 10
    [19-Sep-2023 05:05:13 UTC] User: 
    [19-Sep-2023 05:05:13 UTC] Avatar URL:

    So, it’s empty.

    Plugin Support qtwrk

    (@qtwrk)

    I see, you can not make it ttl=60 , if a guest user visited , it will be cached as empty user id , then next login user accesses it , it retains the empty user id , try make it ttl=0

    Thread Starter Rajon Ahmed

    (@engrrajonahmed)

    Unfortunately, It doesn’t work. I tried it yesterday and I just tried it a few seconds ago.

    Plugin Support qtwrk

    (@qtwrk)

    I tried

    
    function current_author_avatar_shortcode() {
        if ( is_author() ) {
            $user_id = get_queried_object_id(); // Gets the author ID for author archives
        } else {
            $user_id = get_the_author_meta('ID'); // Gets the author ID for single post pages
        }
        $user = get_user_by( 'id', $user_id );
        $first_name = $user->first_name;
        $last_name = $user->last_name;
        $avatar_url = get_user_meta($user_id, 'user_avatar', true);
        error_log( print_r($user, true), 3, $_SERVER['DOCUMENT_ROOT'] . '/output.log' );
        if (empty($avatar_url)) {
            $avatar_url = 'https://static.domain.net/data/static-media/system-default/avatar.png';
        }
        
        $output = '<div class="user-avatar-con"><img class="user-avatar" src="' . $avatar_url . '" alt="' . $first_name . ' ' . $last_name . '"></div>';
        return $output;
    }
    add_shortcode('current_author_avatar', 'current_author_avatar_shortcode');

    where I can see user object is logged into ./output.log

    Thread Starter Rajon Ahmed

    (@engrrajonahmed)

    Yes, I can see that too. But it is not returning with data. It’s weird. Do you have any advice or suggestion for me?

    My goal is to display author meta in the frontend using shortcode using ESI.

    Plugin Support qtwrk

    (@qtwrk)

    But if you got that $user object, then it should be same as any normal php code like without esi

    try dump out all the usermeta, see what it gives, and if your avatar is in it

    • This reply was modified 1 year, 2 months ago by qtwrk.
    Thread Starter Rajon Ahmed

    (@engrrajonahmed)

    Thank you for the suggestion, I will try and investigate further. I will get back here, please don’t close the thread.

    Thread Starter Rajon Ahmed

    (@engrrajonahmed)

    Hi there,

    I figured it out. Now it’s working.

    However, do the ESI-enabled shortcodes [esi shortcode attribute=”value” ttl=”0″] work on a WordPress website powered by CyberPabel + OpenLiteSpeed?

    Because as far as I know ESI is not available on OpenLiteSpeed.

    I use ESI to exclude the shortcodes and prevent them from caching.

    • This reply was modified 1 year, 2 months ago by Rajon Ahmed.
    • This reply was modified 1 year, 2 months ago by Rajon Ahmed.
    Plugin Support qtwrk

    (@qtwrk)

    correct , ESI does not work on OLS , you need to have LiteSpeed enterprise , LiteSpeed ADC , or QUIC cloud CDN to make it work.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘LSCache + ESI Enabled Shortcode’ is closed to new replies.