• I am trying to create a separate tool, that will load WP, so I can run a one time export of some data. I’ve begun the script with simply:

    define('WP_USE_THEMES', false);
    require(__DIR__ .'/wp-blog-header.php');

    When I run it, I get redirected back to the home page of the site.

    I’ve followed the code to try to determine the root cause, but I can only get so far..
    I get to wp-includes/class-wp.php line 726
    It reads:
    do_action_ref_array( 'wp', array( &$this ) );

    If I halt just before this, I do not redirect. If I halt after, it never gets that far, and redirects to the home page instead.

    I am then lost as to what this function does, or how to properly follow after that.

    I’ve tested with all plugins disabled, with a different theme, and with all mu-plugins removed. Still I get the redirect.

    I’m at a loss. I’d be pulling my hair out, if I hadn’t already shaved it off.

    Any assistance or ideas are welcome.

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Instead of fighting WordPress and coaching it into doing things it really does not want to go, suggest that you put your code into a plugin, and run it from the dashboard.

    This plugin is an example of how this can be done, I have used it several times as a starting point.
    https://www.ads-software.com/plugins/wp-csv-to-database/

    Moderator bcworkz

    (@bcworkz)

    Ross is correct, don’t fight it. He didn’t say it, but I will: Requiring wp-blog-header.php in your PHP page is simply Doing it Wrong?. It can be made to work on your own specific site, but initiating the WP environment that way will not work on many WP installations. You are better off just doing it correctly to start with. There are a few options that are considered “correct”.

    Adding a back end page like the Ross’ linked plugin does is one. I personally like to make my custom code into a page template. Add a new WP page based on this template. Requesting the page causes the template code to run. It doesn’t even need to generate output, though some feedback about what’s going on is useful. You can go to the other extreme and make a fully interactive page similar to what a back end page might do. The possibilities are endless.

    Other “correct” possibilities are using Ajax or passing requests through wp-admin/admin-post.php. This last one is similar to Ajax, but no jQuery or JS is required.

    FWIW, the do action line you posted calls any callbacks hooked into the “wp” action, passing the wp object as a parameter by reference. There are few callbacks hooked by default, none of them should cause redirects AFAIK. It’s not worth pursuing because you shouldn’t be using wp-blog-header.php like that anyway.

    Thread Starter ttremain

    (@ttremain)

    I went with writing my export code into a template.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Cannot pinpoint what is causing a redirect’ is closed to new replies.