I'm trying to create a theme settings export script that base_64 encodes the theme options and lets the user download them as a text file.

I've seen a post (What is the best way to export and import theme options?) that involves checking the query vars and then redirecting the user using the 'template_redirect' action. However, it would appear like this action is only available on the frontend of the site (not in the admin). Adding the action to my constructor for the options framework doesn't do anything.

I'm able to get my function to fire by tying it to 'admin_init', but by that time the headers have already been sent (and I'm unable to specify a Content Description). I get a bunch of "Cannot modify header information" warnings and my export string is dumped to the browser instead of being downloaded.

Is there a way I can add a redirect in the WordPress admin, similar to 'template_redirect', that would let me modify the header information before the default headers have been printed?

  • 1
    admin_init shouldn't send headers. if it does, then you might have been generating errors with your code. It's an early level hook. As like init
    – Shazzad
    Commented Dec 15, 2013 at 6:07
  • 2
    Use admin-post.php.
    – fuxia
    Commented Dec 15, 2013 at 10:25

1 Answer 1


Not a direct answer, but: for simple data exports on the admin side, I generally just use the AJAX API. Set up an AJAX handler for your export:

* export from admin
function wpse_126508_export() {
    header('Content-Type: text/xml; charset=utf-8');
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename=wpse_126508_export.xml');

    $xml = new XMLWriter();

    $xml->startDocument('1.0', 'UTF-8');

    // ... your details

    $xml->endElement();     // wpse_126508_export



add_action('wp_ajax_wpse_126508_export', 'wpse_126508_export');
add_action('wp_ajax_nopriv_wpse_126508_export', 'wpse_126508_export');

Then output a link to it on your admin page:

$exportURL = add_query_arg(array(
    'action' => 'wpse_126508_export',
    'nc' => time(),     // cache buster
), admin_url('admin-ajax.php'));
printf('<a href="%s">export</a>', $exportURL);

Of course, you can equally submit a form to the AJAX endpoint if you need to post your request.

Your Answer

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

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