Title: Loading Script Before WordPress
Last modified: August 21, 2016

---

# Loading Script Before WordPress

 *  [JCKnoell](https://wordpress.org/support/users/jcknoell/)
 * (@jcknoell)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/)
 * We need to have a script run before WordPress loads our site, so that, depending
   on sub-domain, users are redirected before our site is loaded. Where can I put
   this script? Is there a place in WordPress for me to do this?

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

 *  Thread Starter [JCKnoell](https://wordpress.org/support/users/jcknoell/)
 * (@jcknoell)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649212)
 * Is there a plugin or anything we can use that will say, “Anything OTHER THAN 
   a select few subdomains will redirect”? All of the ones I’m finding, you need
   to list out the sub domains to redirect. We want everything redirect except www,
   blank, beta, stage, etc. Everything else should redirect to our app login… Thoughts?
 * We can go the htaccess route, but a plugin would be a lot easier.
 * Thanks!
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649216)
 * Ok – let’s think about this for a moment…
 * How is _an_y plugin going to run before WordPress has loaded?
 *  Thread Starter [JCKnoell](https://wordpress.org/support/users/jcknoell/)
 * (@jcknoell)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649228)
 * Sorry, I should specify, Before the CONTENT from the website loads. It’s okay
   if WordPress loads, but we don’t want them to see anything on the sales site 
   before it sends them to the app.
 * I guess that’s what you mean though, right? I have never really understood the
   way WordPress works…
 * Thanks for the clarification.:)
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649231)
 * I’d imagine that some of the redirection plugins load before the site’s content
   is generated but I suspect that some (if not all) do so by modifying the site’s.
   htaccess file. Given that your needs appear to be fairly bespoke, you might want
   to have a look at some of them to see if you can re-use their code to your benefit.
 *  Thread Starter [JCKnoell](https://wordpress.org/support/users/jcknoell/)
 * (@jcknoell)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649233)
 * Thanks. I appreciate the input! My database manager is probably going to just
   go with the htaccess modifications.
 *  [Titanium Creative](https://wordpress.org/support/users/titanium-creative/)
 * (@titanium-creative)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649333)
 * If a redirect script runs on a hook prior to content generation, wouldn’t wordpress,
   by default, skip further processing?
 * It seems like it would be a huge waste to continue after a redirect has been 
   encountered.
 * The original question was running before anything had loaded but it was clarified
   to mean before any CONTENT had loaded. By definition a redirect has to be sent
   prior to any other headers/content.
 * Something as simple as:
 * // Array of subs for no redirect
    // $subs = array (‘list’, ‘of’ , ‘no-redirect’,‘
   subdomains’); // Get url subdomain // $sub = array_shift(explode(“.”,$_SERVER[‘
   HTTP_HOST’])); // $location = ‘redirect.to.this.url’; // If subdomain is not 
   in array -> Redirect // if (!in_array($sub, $subs) { wp_redirect( $location, 
   $status ); exit(); }
 * hooked into init should do it.
    // add_action(‘init’,’function_from_above’,1);
 * Just add something like that to wherever you have your custom scripts(well, actually
   program it first, but that’s the meat of it). I have a plugin skeleton I use 
   for simple edits that don’t belong in my custom child theme.
 * [http://codex.wordpress.org/Plugin_API/Action_Reference](http://codex.wordpress.org/Plugin_API/Action_Reference)
   
   [https://codex.wordpress.org/Function_Reference/wp_redirect](https://codex.wordpress.org/Function_Reference/wp_redirect)
 *  [TheHandOfCod](https://wordpress.org/support/users/chrisodell/)
 * (@chrisodell)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649346)
 * In case anyone else stumbles across this thread I would just like to add my vote
   to Titanium Creative’s answer. Having a code snippet akin to the pseudo code 
   provided by TC seems a simpler solution than modifying .htaccess files which 
   always cause me pain.

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

The topic ‘Loading Script Before WordPress’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [script](https://wordpress.org/support/topic-tag/script/)
 * [server](https://wordpress.org/support/topic-tag/server/)
 * [subdomain](https://wordpress.org/support/topic-tag/subdomain/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 4 participants
 * Last reply from: [TheHandOfCod](https://wordpress.org/support/users/chrisodell/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/loading-script-before-wordpress/#post-4649346)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
