Get Wordpress plugins to work within template_part files

18 Views Asked by At

I am writing a plugin to use dompdf to generate a pdf file from a template file in Wordpress. The template file calls a WooCommerce product by it's ID and then populates the html template with it's data before outputting it as a PDF.

In my plugin file I have this code to call the template file:

function pdf_content(){
    ob_start();
    $c = get_template_part( 'gddompdf/gddompdf' );
    return ob_get_clean() . $c;
}

Then in the theme file I have the folder "gddompdf" which contains the template file, "gddompdf.php".

Now, basic Wordpress functionality seems to work find in the template file. I.e., If I call the product as a post by it's ID:

$post = get_post($post_id);
$post_meta = get_post_meta($post_id);

These lines both work fine and the $post and $post_meta variables populate with arrays of all the product data.

However, if I try to use functions from other plugins that are installed in Wordpress, such as WooCommerce and ACF Pro, I get fatal errors.

i.e.,:

$product = new WC_Product( $post_id );

This line results in "Fatal error: Uncaught Error: Class "WC_Product" not found in...". I can only assume this is because WooCommerce is not loading for the template file to be able to access it's functions.

And for ACF, "get_field()" seems to work fine, but for repeater fields:

$rows = get_field('extra_information_tabs');
        // loop through the rows of data
        foreach($rows as $row){

echoing $row['(field name)'] is returning absolutely nothing.

If I try:

print_r($rows);

to see what data it's getting, it just returns "13". However, if I run this same code in a standard Wordpress page template, it works fine.

Any idea where I might have gone wrong here?

0

There are 0 best solutions below