I am creating a plugin and I need to add a menu entry that forces a file obtain (a CSV).
There are just a few posts on this discussion board and over the web with an identical resolution, all of them are fairly outdated and that resolution appears to not work anymore.
That is my present strategy (primarily based on the talked about posts). This does drive a file obtain, however the file is definitely a generated HTML with all WP admin menu and all the things.
MyPlugin::__construct()
$functionality = 'manage_options';
$menu_slug = 'myplugin-data-export';
$operate = [$this, 'exportCSVdata'];
add_submenu_page(null, '', '', $functionality, $menu_slug, $operate);
MyPlugin::exportCSVdata()
public operate exportCSVdata()
{
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=data.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo 'knowledge';
exit();
}
This operate is executed after I enter the URL http://instance.com/wp-admin/admin.php?web page=myplugin-data-export
So, the query possibly is,
how can I create a operate inside my plugin class that generates a unadorned web page? With no header, consists of and all of the WP html stuff?
In some ohter posts I noticed a proposal to make use of admin-post.php
as a substitute of admin.php
however did not work both.
EDIT
Plugin is being run as follows:
if ( !class_exists( 'MyPlugin' ) ) {
class MyPlugin
{
static $occasion = false;
public static operate getInstance()
{
if ( !self::$occasion )
self::$occasion = new self;
return self::$occasion;
}
public operate __construct()
{
register_activation_hook( __FILE__, [$this, 'mypluginInstall']);
add_action('admin_menu', [$this, 'adminMenuEntries']);
}
public operate adminMenuEntries()
{
//... some stuff
$functionality = 'manage_options';
$menu_slug = 'myplugin-data-export';
$operate = [$this, 'exportCSVdata'];
add_submenu_page(null, '', '', $functionality, $menu_slug, $operate);
}
}
}