0

I'm writing a simple plugin, for which I have created a simple settings page accessible via the menu. In this settings page, I would like - among other things - to have a button to download a file created by a function in my PHP script.

add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
    add_menu_page('Plugin management', 'Plugin management', 'manage_options', 'my_plugin-plugin-settings', 'my_plugin_settings_page', 'dashicons-admin-generic');
}


function my_plugin_settings_page() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    ?>
        <p>Please click the button below to download the file.</p>
        <button id="export-file">Download the file</button>

    </div>
    <?php
}

function trigger_download() {
// This function should build the file (for instance a simple .txt) and trigger the download dialog in the user's browser.
    }

I'm confused as to what I need to do next. I saw that I have to set specific headers such as those :

header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename={$file_name}" );
header( "Content-Type: application/json; charset=utf-8" );

But I don't know what kind of action should be called when the button is clicked (I don't know much about forms and GET POST methods in general, but I'm happy to learn).

  • 1
    See answers to the question How can I force a file download in the WordPress backend?. There are good ideas to start with. – Max Yudin Jul 10 '19 at 12:53
  • @MaxYudin The answer to the question you pointed to works for a file which already exists. I'm looking for a solution which involves building the file dynamically in a function before offering for download. Should I edit my question to make this clearer ? – GuitarExtended Jul 11 '19 at 9:51
1

I managed to get the behaviour I was looking for with the following code :

add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
    add_menu_page('Plugin management', 'Plugin management', 'manage_options', 'my_plugin-plugin-settings', 'my_plugin_settings_page', 'dashicons-admin-generic');
}


function my_plugin_settings_page() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    ?>
        <p>Please click the button below to download the file.</p>
        <form action="http://my-website-url/wp-admin/admin-post.php?action=add_foobar&data=foobarid" method="post">
          <input type="hidden" name="action" value="add_foobar">
          <input type="hidden" name="data" value="foobarid">
          <input type="submit" value="Download the file">
        </form>

    </div>
    <?php
}

// From : https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
add_action( 'admin_post_add_foobar', 'prefix_admin_add_foobar' );

function prefix_admin_add_foobar() {
    // Handle request then generate response using echo or leaving PHP and using HTML
    $filename = "file.csv";
    $array = array(array(1,2,3,4), array("merle","mésange","pie","coucou"));
    $delimiter = ",";

    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'";');

    // open the "output" stream
    // see http://www.php.net/manual/en/wrappers.php.php#refsect2-wrappers.php-unknown-unknown-unknown-descriptioq
    $f = fopen('php://output', 'w');

    foreach ($array as $line) {
        fputcsv($f, $line, $delimiter);
    }
    die();
}

The admin_post_(action) hook was very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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