我正在尝试创建一个插件,使用FPDF创建一个选项页面来打印自定义帖子类型。
这是我的代码:
<?php
/*
* Plugin Name: RT FPDF Tutorial
* Description: A plugin created to demonstrate how to build a PDF document from WordPress posts.
* Version: 1.0
*/
if ( ! defined( \'ABSPATH\' ) ) {
exit;
}
include( \'pdf-helper-functions.php\');
$pdf = new PDF_HTML();
if( isset($_POST[\'generate_posts_pdf\'])){
output_pdf();
}
add_action( \'admin_menu\', \'rt_fpdf_create_admin_menu\' );
function rt_fpdf_create_admin_menu() {
$hook = add_submenu_page(
\'tools.php\',
\'PDF Generator\',
\'PDF Generator\',
\'manage_options\',
\'rt-fdpf-tutorial\',
\'rt_fpdf_create_admin_page\'
);
}
function rt_fpdf_create_admin_page() {
echo \'<div class="wrap">\';
echo \' <form method="post" id="as-fdpf-form"><button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate the Directory</button></form>\';
echo \'</div>\';
}
function output_pdf() {
global $pdf;
$title_line_height = 10;
$content_line_height = 8;
$args = array(
\'post_type\' => \'cc_family\',
);
// The Query
//$the_query = new WP_Query( array (\'post_type\' => \'cc_family\') );
$pdf->AddPage();
$pdf->SetFont( \'Arial\', \'\', 42 );
$pdf->Write(20, \'No families to make a directory yet\');
$pdf->Output(\'D\',\'new-directory.pdf\');
exit;
}
由于代码现在可以正常工作($u查询被注释掉)将创建一个pdf,内容为“;没有族"E;。
如果我取消对上面的行的注释以开始添加查询,我的站点会出现一个严重错误。如果添加整个通用查询,也会出现错误。
我尝试了get\\u posts(),这很有效,但它似乎不允许我提取自定义字段。添加我错过的新查询有什么特别之处吗?
更新以添加错误:
> [13-Sep-2021 15:30:51 UTC] PHP Notice: ob_end_flush(): failed to send
> buffer of zlib output compression (0) in
> /home/whn/public_html/wp-includes/functions.php on line 5107
> [13-Sep-2021 15:30:53 UTC] PHP Fatal error: Uncaught Error: Call to
> undefined function is_user_logged_in() in
> /home/whn/public_html/wp-includes/class-wp-query.php:2544 Stack
> trace:
> #0 /home/whn/public_html/wp-includes/class-wp-query.php(3465): WP_Query->get_posts()
> #1 /home/whn/public_html/wp-includes/class-wp-query.php(3576): WP_Query->query(Array)
> #2 /home/whn/public_html/wp-content/plugins/pdfer/rt_pdf.php(48): WP_Query->__construct(Array)
> #3 /home/whn/public_html/wp-content/plugins/pdfer/rt_pdf.php(18): output_pdf()
> #4 /home/whn/public_html/wp-settings.php(409): include_once(\'/home/wh3n/publ...\')
> #5 /home/whn/public_html/wp-config.php(91): require_once(\'/home/wh3n/publ...\')
> #6 /home/whn/public_html/wp-load.php(50): require_once(\'/home/wh3n/publ...\')
> #7 /home/whn/public_html/wp-admin/admin.php(34): require_once(\'/home/wh3n/publ...\')
> #8 /home/whn/public_html/wp-admin/tools.php(40): require_once(\'/home/wh3n/publ...\')
> #9 {main} thrown in /home/whn/public_html/wp-includes/class-wp-query.php on line 2544
> [13-Sep-2021 15:30:53 UTC] PHP Notice: is_embed was called
> <strong>incorrectly</strong>. Conditional query tags do not work
> before the query is run. Before then, they always return false. Please
> see <a
> href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging
> in WordPress</a> for more information. (This message was added in
> version 3.1.0.) in /home/whn/public_html/wp-includes/functions.php on
> line 5663 [13-Sep-2021 15:30:53 UTC] PHP Notice: is_search was called
> <strong>incorrectly</strong>. Conditional query tags do not work
> before the query is run. Before then, they always return false. Please
> see <a
> href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging
> in WordPress</a> for more information. (This message was added in
> version 3.1.0.) in /home/whn/public_html/wp-includes/functions.php on
> line 5663 [13-Sep-2021 15:30:54 UTC] PHP Notice: ob_end_flush():
> failed to send buffer of zlib output compression (0) in
> /home/whn/public_html/wp-includes/functions.php on line 5107
最合适的回答,由SO网友:Jacob Peattie 整理而成
问题是你的output_pdf()
函数调用太早。您在主插件文件的根目录下运行该函数,这意味着它在插件加载后以及WordPress完成加载之前运行。
您至少需要将其挂起以稍后运行,例如在admin_init
挂钩:
function rt_fpdf_generate_pdf() {
include( \'pdf-helper-functions.php\');
$pdf = new PDF_HTML();
if( isset($_POST[\'generate_posts_pdf\'])){
output_pdf();
}
}
add_action( \'admin_init\', \'rt_fpdf_generate_pdf\' );
更好的是,您可以使用
admin_post_
挂钩:
https://developer.wordpress.org/reference/hooks/admin_post_action/还要注意代码中的其他一些问题。希望它们只是代码不完整的结果,但我需要注意它们,以防有人认为他们可以复制代码:
您需要使用名称空间,或者至少为函数添加前缀。output_pdf()
太笼统,可能会导致冲突没有使用nonce或权限检查来确保只有允许的用户才能生成此PDF。如文所述,任何人都可以通过强制生成数千个PDF来提交帖子请求并关闭你的网站