Ready to get started?Download WordPress


Loading Script Before WordPress (9 posts)

  1. JCKnoell
    Posted 6 months ago #

    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?

  2. TheHandOfCod
    Posted 6 months ago #

    I think that the only way of doing this before WordPress loads is using .htaccess.

    However there are a number of plugins which will perform a 301 redirect for you if you do not mind WordPress loading first.

    An article on 301 redirects can be found here

    and a list of possible plugins can be found here

  3. JCKnoell
    Posted 6 months ago #

    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.


  4. esmi
    Forum Moderator
    Posted 6 months ago #

    Ok - let's think about this for a moment...

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

  5. JCKnoell
    Posted 6 months ago #

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

  6. esmi
    Forum Moderator
    Posted 6 months ago #

    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.

  7. JCKnoell
    Posted 6 months ago #

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

  8. Titanium Creative
    Posted 5 months ago #

    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.


  9. TheHandOfCod
    Posted 4 months ago #

    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.


You must log in to post.

About this Topic