在Admin Dashboard中重写默认的Wordpress行为,用于发送带有CSV生成的标题


Overriding default Wordpress behavior in Admin Dashboard for sending headers with CSV generation

我正在制作一个WP插件的原型,使用plugin Boilerplate。我发现很难在WP IOT中生成文档和覆盖管理仪表板,创建pdf/电子表格等。我肯定有我不知道的诀窍。

如果有最佳实践,我很想知道,因为我将采取的传统方法(与其他框架)似乎不起作用。

我现在正在做的是生成一个菜单页,其中包含一个子菜单页。子菜单页具有生成各种报告的链接。在阅读了Codex后,建议添加一个子页面菜单页面,父页面为空(因此未列出,但可用)。

代码如下:

$this->plugin_screen_hook_suffix = add_submenu_page(
            null,
            __( 'Session', $this->plugin_slug. '-reports' ),
            __( 'Session Report', $this->plugin_slug. '-reports' ),
            'manage_options',
            $this->plugin_slug . '-report-session',
            array( $this, 'display_plugin_session_report_page' )
        );
public function display_plugin_session_report_page() {
        header('Content-Description: File Transfer');
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename=report.csv;');
        header('Pragma: no-cache');
        header('Expires: 0');
//push out csv...
}

我有这个工作与WP的唯一途径是链接到一个外部页面,如报告。php,并调用其中的方法。还有别的办法吗?

这是我采用的解决方案,它避免了设置单独的脚本页面:

在URL中,我添加了以下内容:&noheader=true,然后在函数调用中,我使用ob_clean清除输出缓冲区并生成我自己的头。我无法找到任何关于noheader设置的可靠文档,但发现它只适用于admin.php调用。

我希望这对其他开发人员有所帮助!