Download button in a plugin’s settings page

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>


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).

Read more here:: Download button in a plugin’s settings page

Leave a Reply

Your email address will not be published. Required fields are marked *