0

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-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;
}

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
> <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
2
  • What's the critical error? Sep 13, 2021 at 15:58
  • @JacobPeattie error message added. Thank you
    – rudtek
    Sep 13, 2021 at 16:06

1 Answer 1

1

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.
3
  • Thank you Jacob! I'll test this out. Out of Curiosity, why would get_posts() work but not wp_query? And Also thanks for the note on security. Yes this is the simplified code to just have the nitty gritty for the question here.
    – rudtek
    Sep 13, 2021 at 16:33
  • The problem is that the is_user_logged_in() function has not been loaded yet. WP_Query() only calls this function if the post_status argument has not been passed. When you use get_posts() it passes a default post_status to WP_Query. Sep 13, 2021 at 16:40
  • This worked. I'll pursue admin_post as well. Thank you for the articulate response and additional information.
    – rudtek
    Sep 13, 2021 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.