Support » Plugin: LiteSpeed Cache » Help with ESI API PHP Code (Not Shortcode)

  • Resolved Jacob Hill

    (@tekfused)


    Hello,

    I’m attempting to build a ESI piece of code that I can use to check a user’s subscription. I got it working as a shortcode, but I want to run it using WP’s init hook.

    The main issue is that the subscription_check() function is never called by the LiteSpeed_Cache_API::hook_tpl_esi(‘subscription_check_esi’ … command. Here is my code:

    add_action( 'init', 'esi_subscription_check' );
    function esi_subscription_check()
    {
        return LiteSpeed_Cache_API::esi_url( 'subscription_check_esi', 'Subscription Check' );
    }
    
    LiteSpeed_Cache_API::hook_tpl_esi('subscription_check_esi', 'subscription_check' ) ;
    
    function subscription_check()
    {
    	$current_user = wp_get_current_user();
    	write_log($current_user->user_email. ' | Page: ' . get_the_title() . ' | UserTime is '.time());
    	// Do stuff here.
    }

    I must be missing something simple. Thanks in advance!

    Jacob Hill

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support stanley@litespeed

    (@stanleylitespeed)

    Hi @tekfused, if you hook the esi_url on init, it should not print our anything on the frontend so the function will not work.

    May I know normally how do you run the subscription_check function? On functions.php or somewhere?

    Hi @stanleylitespeed!

    FYI, I’ve generated a LS Cache report for you – HTWNZAJH.

    Thanks for the quick reply. In this case, I’m not trying to print/echo anything on the screen, I just want to use ESI to run some PHP which will check to see if the user has an active WooCommerce Subscription, and then redirect to a “Renew Your Membership” page if their subscription has expired.

    The write_log function in the snippet is another function that writes to the debug.log file. It seems LiteSpeed_Cache_API::hook_tpl_esi never calls the subscription_check() function, because I don’t see any new adds to the debug.log file.

    And yes, I use my child theme’s function.php file.

    I forgot to include that I do have the set_ttl command as well:

    add_action( 'init', 'esi_subscription_check' );
    function esi_subscription_check()
    {
        return LiteSpeed_Cache_API::esi_url( 'subscription_check_esi', 'Subscription Check' );
    }
    
    LiteSpeed_Cache_API::hook_tpl_esi('subscription_check_esi', 'subscription_check' ) ;
    
    function subscription_check()
    {
    	LiteSpeed_Cache_API::set_ttl( 15 ) ;
    	$current_user = wp_get_current_user();
    	write_log($current_user->user_email. ' | Page: ' . get_the_title() . ' | UserTime is '.time());
    	// Do stuff here.
    }

    Thanks again for the help!

    Jacob Hill

    • This reply was modified 4 months, 1 week ago by Jacob Hill.
    Plugin Support stanley@litespeed

    (@stanleylitespeed)

    Hi Jacob, the ESI function require you to print something on the frontend to make it works, you can check on this article for more details.

    In your situation, do you need a server-side redirection for an expired subscription? If not, we suggest you convert the checking code as an Ajax call.

    Hi @stanleylitespeed, then if I echo something in addition to the code I’m processing, it should work?

    Yes, it does need to be server side, because the user’s role may need to be updated, as well as potential calls to a 3rd party API.

    Quick update: it seems that using the init, wp_loaded, and wp hooks do not work. I’ve only had success with the wp_head hook. wp_redirect() does not work though.

    Here is a simplified snippet:

    add_action( 'wp_head', 'esi_time' );
    function esi_time()
    {
        echo LiteSpeed_Cache_API::esi_url( 'show_your_time', 'xxx Name' ) ;
    }
    
    LiteSpeed_Cache_API::hook_tpl_esi('show_your_time', 'show_your_time' ) ;
    function show_your_time( $param )
    {
        LiteSpeed_Cache_API::set_ttl( 15 ) ;
        
        $user_ID = get_current_user_id();
        $wp_user_obj = new WP_User( $user_ID );
        
        $current_user = wp_get_current_user();
        $wp_user_obj->set_role( 'subscriber' );
        echo date("h:i:sa") ;
    }

    Any idea why the other hooks won’t work?

    • This reply was modified 4 months, 1 week ago by Jacob Hill.
    Plugin Support stanley@litespeed

    (@stanleylitespeed)

    Hi @tekfused, unfortunately, ESI cannot do a server-side redirect because ESI is not a direct response to the user’s browser, so wp_redirect does not work.

    In this case, you can 1) set private cache for the logged-in user with a shorter TTL, 2) use ESI to print a JS script to do a client-side redirect, 3) Make you subscription checking as an AJAX.

    2 and 3 will have better performance.

    Jacob Hill

    (@tekfused)

    Hi @stanleylitespeed, thank you very much for getting back with me! I went with option 2, echoing a JS redirect.

    My last question here is why doesn’t the “init” and other hooks work? The only hook that I could get to work was “wp_head.”

    • This reply was modified 4 months ago by Jacob Hill.
    Plugin Support stanley@litespeed

    (@stanleylitespeed)

    Hi @tekfused, add_action to init for ESI should also work, may I know what issue are you facing? Remember to purge all cache when you editing the code, sometimes it might be cached the old version.

    Jacob Hill

    (@tekfused)

    Hi @stanleylitespeed, when I change the hook from wp_head to init, this text appears at the top of the page: [an error occurred while processing this directive]

    As I mentioned, I tried a few other hooks, and wp_head is the only one that worked.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.