I am writing a plugin and want to force users to download a file immediately when they activate the plugin. Here is the code that I've written so far but doesn't work.
No fatal errors but gives warning "The plugin generated 94 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin."
// Downloader
function dcg_file_downloader() {
$file = ABSPATH . 'wp-content/uploads/sample.txt';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
print $file;
}
// Plugin activation hook
function dcg_activate_plugin() {
dcg_file_downloader();
update_option('dcg_plugin_version', '1.0');
}
register_activation_hook( __FILE__, 'dcg_activate_plugin' );
Purpose of achieving this is to backup a specific file (for eg.: .htaccess) before making any changes into it.
Also, Is it a good idea to use ABSPATH to get WordPress installation directory/path?
header('Location: ');
?header('Location: ');
doesn't work.headers_sent()
to see where they've been sent, then back trace from there. php.net/manual/en/function.headers-sent.php