Support » Fixing WordPress » 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)
  • 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.




    Forum Moderator

    Ok – let’s think about this for a moment…

    How is any plugin going to run before WordPress has loaded?

    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.:)



    Forum Moderator

    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.

    Thanks. I appreciate the input! My database manager is probably going to just go with the htaccess modifications.

    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 = ‘’;
    // 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.

    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.