0

I am building a specialized xml output script (can't use the built in export) and I have a file in the web root that I can call from the URL domain.com/myscript.php. However, I need to be able to do wordpress DB calls to get to the data. I have read you can load wp-load.php in the script - will that be enough?

Maybe there is some other (better) way of doing this but wordpress sends headers so I can't output a clean xml file without the wordpress clutter (header footer etc).

1 Answer 1

1

You could do that, including wp-load.php would bootstrap WordPress and allow you to query the database. I prefer to keep things encapsulated in plugins though, much more portable. You just need to hook early enough to send your own headers, and you can short-circuit the rest of the load process. For example-

function wpd_my_export_script() {
    if( isset( $_GET['do_my_export_stuff'] ) ) {
        header('Content-Type: application/xml; charset=utf-8');
        // do your database stuff
        exit;
    }
}
add_action( 'wp_loaded', 'wpd_my_export_script' );

Then visiting http://example.com/?do_my_export_stuff will trigger your code.

7
  • Milo, Thanks for the reply. I think what you suggest is what I'm looking for. However I have a query string in the URL like domain.com/myscript.php?lang=eng&output=xml&type=short. How would I handle that when visiting the URL.
    – Chris
    Mar 26, 2015 at 20:55
  • Just append the extra GET vars to the end of the example URL I gave, but make sure you use GET vars that don't clash with WordPress reserved terms, otherwise you may trigger unexpected things.
    – Milo
    Mar 26, 2015 at 21:02
  • Do you mean like this: domain.com/?do_my_export_stuff?lang=eng&output=xml&type=short
    – Chris
    Mar 26, 2015 at 21:07
  • query strings start with a single ?, multiple vars are separated by a &, so domain.com/?do_my_export_stuff&lang=eng&output=xml&type=short. but note that WordPress already uses type, you may need to change that.
    – Milo
    Mar 26, 2015 at 21:20
  • THANKS MILO!!! I put that code in a plugin file and it works a treat. I'll fix the vars so they are unique.
    – Chris
    Mar 26, 2015 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.