Error in pdf generating plugin using FPDF

I'm trying to create a plugin using FPDF to create an options page to print custom post types.

Here is my code:

?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-formbutton class=button button-primary type=submit name=generate_posts_pdf value=generateGenerate 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;
}

As the code is right now it works ($the_query is commented out.) A pdf is created with the content no families....

If I un-comment the line above to start adding the query, I get a critical error on my site. If I add an entire generic query I get an error as well.

I've tried with get_posts() and this works, but it doesn't seem to allow me to pull custom fields. Is there something special to adding a new query I've missed?

Update to add the error:

 [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
 strongincorrectly/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
 strongincorrectly/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

Topic wp-query pdf plugins Wordpress

Category Web


The problem is that your output_pdf() function is being called too early. You have the function running in the root of the main plugin file, meaning that it runs as soon as the plugin is loaded, and before before WordPress has finished loading.

You need to at least hook it to run later, such as on the admin_init hook:

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' );

Better yet, you could use the admin_post_ hook: https://developer.wordpress.org/reference/hooks/admin_post_action/

Also note some other issues with your code. They are hopefully just a result of the code being incomplete, but I need to note them in case anybody comes along thinking they can copy the code:

  • You need to use namespaces, or at least prefix your functions. output_pdf() is too generic, and could result in conflicts.
  • There's no use of nonces or permission checks to ensure only allowed users can generate this PDF. As written anybody could submit a post requests and bring down your site by forcing it to generate thousands of PDFs.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.