Help with ESI API PHP Code (Not Shortcode)




Wordpress LScache Plugin: Help with ESI API PHP Code (Not Shortcode)

Last Updated on: Wed, 15 Apr 2026 00:00:02
Hello, Im attempting to build a ESI piece of code that I can use to check a users subscription. I got it working as a shortcode, but I want to run it using WPs 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 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, Ive generated a LS Cache report for you ? HTWNZAJH. Thanks for the quick reply. In this case, Im 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 dont see any new adds to the debug.log file. And yes, I use my child themes 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 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 Im processing, it should work? Yes, it does need to be server side, because the users 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. Ive 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 ) Expand Any idea why the other hooks wont work? Hi @tekfused, unfortunately, ESI cannot do a server-side redirect because ESI is not a direct response to the users 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. 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 doesnt the init and other hooks work? The only hook that I could get to work was wp_head. 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. 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.



LiteCache Rush: Speed comes from using less, not from doing it faster



Reference