Title: Setup for late init hook ?
Last modified: September 5, 2016

---

# Setup for late init hook ?

 *  Resolved [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/)
 * Hi! I have a client with this plugin which needs some functions to run at init
   hook or a hook near init.
    On WP Supercache or W3 total cache there are option
   to run in PHP legacy mode or after init hook. There is something similar on Litespeed
   cache plugin that will let me run those functions before the page cache is generated?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Author [LiteSpeed Technologies](https://wordpress.org/support/users/litespeedtech/)
 * (@litespeedtech)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8148894)
 * Hi Damian,
 * Do you have any specific requirements for the hook? E.g. only when the user is
   logged in, only when user is visiting admin page, etc.
 * For all visitors and pages, we have an action litespeed_cache_detect_thirdparty.
 * If it is an ajax request, it will run at the after_setup_theme hook.
    Else if
   it is an admin page, it will run at the admin_init hook. For all other requests,
   this will run at the wp hook.
 * Will this suffice?
 * Cheers,
    Kevin
 *  Thread Starter [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8148992)
 * Nothing special. I need to run some checks with the [Geotargeting plugin](https://timersys.com/plugins/geotargeting-pro/)
   that runs at init hook and then perform redirection of the user . I just tried
   and works perfectly
 * Thanks!
 *  Plugin Author [LiteSpeed Technologies](https://wordpress.org/support/users/litespeedtech/)
 * (@litespeedtech)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8149005)
 * Glad to hear it!
 * We do plan to make some changes to the thirdparty integration, but I will make
   a note to confer with you if we change this hook in any way.
 * Cheers,
    Kevin
 *  Thread Starter [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8150694)
 * I spoke to soon. I made more tests and it seems that is not working fine. It 
   seems that the hook is not fired on every page load.
 * For example I use one browser with VPN and visit a page and If I use another 
   browser without the vpn the user is not being redirected as if they both were
   from same country.
 * IF you want to better understand what Im doing please take a look to the following:
 *     ```
       add_action( 'litespeed_cache_detect_thirdparty', 'geot_custom_redirects');
   
        function geot_custom_redirects() {
           global $geot;
   
   
           $whitelist = array(
   
               #'24.232.111.111',
       	); 
   
           // if main plugin is not enabled return
           if( ! function_exists( 'geot_target' ) )
               return;
   
           // if search engine exit    
           if( $geot->functions->isSearchEngine() )
               return;
   
           // if is admin  return    
           if( current_user_can('administrator') && ! isset( $_GET['debug_geot'] ) )
           	return;
   
           // if is admin page return
           if( is_admin() )
               return;
   
       	// If user IP is whitelisted return
       	$ip = $geot->functions->getUserIP();
   
       	if( in_array( $ip, $whitelist ) )
       		return;
   
       	if( isset( $_GET['timersys_debug_geot'] )) {
       		echo '<pre>';
               var_dump(geot_country_name());
       		var_dump(geot_target( '', 'AU_COUNTRIES'));
       		echo '</pre>';
       		die();
       	}		    
           if( ! geot_target( '', 'AU_COUNTRIES') ) {
           	$url =  "{$_SERVER['REQUEST_URI']}";
       		$escaped_url = 'https://xxxxxxxx.com' . htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );	
             	wp_redirect( $escaped_url );
             	die();
           }
        }   
       ```
   
 * Browser A with Australia vpn generates the page cache.
    Browser B visitst the
   site and can browse around. Once all cache is purged browser B gets redirected
   normally again.
 * Any clue?
 *  Plugin Author [LiteSpeed Technologies](https://wordpress.org/support/users/litespeedtech/)
 * (@litespeedtech)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8150787)
 * Hi Damian,
 * As it looks like you are using geo-ip related functionality, you may need to 
   do something with the rewrite rules.
 *  RewriteEngine on
    RewriteRule .* – [E=Cache-Control:vary=%{ENV:GEO_COUNTRY}]
 * This will add a vary for each country that visits using geoip databases (essentially,
   marking each country as a different type of user).
 * If my answer is incorrect, I think I misunderstood what you mean, so further 
   clarification could be useful.
 * Cheers,
    Kevin
 *  Thread Starter [Damian](https://wordpress.org/support/users/timersys/)
 * (@timersys)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8151421)
 * Im using a plugin to calculate the country, Not the server so that’s not going
   to work.
    I just need the code on my previous message to run on every page load
   on litespeed_cache_detect_thirdparty
 * Regards
 *  Plugin Author [LiteSpeed Technologies](https://wordpress.org/support/users/litespeedtech/)
 * (@litespeedtech)
 * [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8154855)
 * Hi Damian,
 * The cache operates by storing the php output as a static file. The server will
   not enter php if the page is in the cache, so no hooks will run on those pages.
 * As the rewrite rules operate at the server level (before checking the cache),
   the rules can check the countries without needing to enter php.
 * I recommend you try to use a server level Geo IP system because it can improve
   your server’s performance if you have many requests that need to be redirected.
   By using the plugin, each of those requests are forced to enter php at least 
   once, then again when redirected.
 * I hope this is helpful, let me know if my understanding is still incorrect, or
   if you are still confused 🙂
 * Cheers,
    Kevin

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Setup for late init hook ?’ 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/)

 * 7 replies
 * 2 participants
 * Last reply from: [LiteSpeed Technologies](https://wordpress.org/support/users/litespeedtech/)
 * Last activity: [9 years, 8 months ago](https://wordpress.org/support/topic/setup-for-late-init-hook/#post-8154855)
 * Status: resolved