Title: Help with ESI API PHP Code (Not Shortcode)
Last modified: September 20, 2019

---

# Help with ESI API PHP Code (Not Shortcode)

 *  Resolved [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/)
 * 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)

 *  [stanley@litespeed](https://wordpress.org/support/users/stanleylitespeed/)
 * (@stanleylitespeed)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11952624)
 * Hi [@tekfused](https://wordpress.org/support/users/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?
 *  Thread Starter [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11953145)
 * Hi [@stanleylitespeed](https://wordpress.org/support/users/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 6 years, 8 months ago by [Jacob Hill](https://wordpress.org/support/users/tekfused/).
 *  [stanley@litespeed](https://wordpress.org/support/users/stanleylitespeed/)
 * (@stanleylitespeed)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11953314)
 * Hi Jacob, the ESI function require you to print something on the frontend to 
   make it works, you can check on [this article](https://blog.litespeedtech.com/2017/09/06/wpw-esi-and-litespeed-cache/)
   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.
 *  Thread Starter [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11953361)
 * Hi [@stanleylitespeed](https://wordpress.org/support/users/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.
 *  Thread Starter [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11953892)
 * 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 6 years, 8 months ago by [Jacob Hill](https://wordpress.org/support/users/tekfused/).
 *  [stanley@litespeed](https://wordpress.org/support/users/stanleylitespeed/)
 * (@stanleylitespeed)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11964910)
 * Hi [@tekfused](https://wordpress.org/support/users/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.
 *  Thread Starter [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11965772)
 * Hi [@stanleylitespeed](https://wordpress.org/support/users/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 6 years, 8 months ago by [Jacob Hill](https://wordpress.org/support/users/tekfused/).
 *  [stanley@litespeed](https://wordpress.org/support/users/stanleylitespeed/)
 * (@stanleylitespeed)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11972498)
 * Hi [@tekfused](https://wordpress.org/support/users/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.
 *  Thread Starter [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * (@tekfused)
 * [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11973982)
 * Hi [@stanleylitespeed](https://wordpress.org/support/users/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)

The topic ‘Help with ESI API PHP Code (Not Shortcode)’ is closed to new replies.

 * ![](https://ps.w.org/litespeed-cache/assets/icon-256x256.png?rev=2554181)
 * [LiteSpeed Cache](https://wordpress.org/plugins/litespeed-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/litespeed-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/litespeed-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/litespeed-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/litespeed-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/litespeed-cache/reviews/)

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * 9 replies
 * 2 participants
 * Last reply from: [Jacob Hill](https://wordpress.org/support/users/tekfused/)
 * Last activity: [6 years, 8 months ago](https://wordpress.org/support/topic/help-with-esi-api-php-code-not-shortcode/#post-11973982)
 * Status: resolved