Loading Script Before WordPress (8 posts)

  1. JCKnoell
    Posted 1 year 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. JCKnoell
    Posted 1 year 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.


  3. esmi
    Forum Moderator
    Posted 1 year ago #

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

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

  4. JCKnoell
    Posted 1 year 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.:)

  5. esmi
    Forum Moderator
    Posted 1 year 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.

  6. JCKnoell
    Posted 1 year ago #

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

  7. Titanium Creative
    Posted 1 year 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.


  8. TheHandOfCod
    Posted 1 year 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.

Topic Closed

This topic has been closed to new replies.

About this Topic