admin管理员组

文章数量:1122846

I'm trying to create a plugin that will print the current custom post type for the currently viewed post type.

the idea is that if a user visits one of my custom post types (products) they will be able to click a button on that "page" and it will use DomPDF to print the page.

All my code is in a plugin except for the call for the funtion which is in myCHILDtheme/single-rt_products.php.

The button function is placed in my single-rt_products.php:

rt_dom_create_download_button();

this calls a function in my plugin main php file:

function rt_dom_create_download_button() {
    echo '<div class="wrap">';
    echo '<form method="post" id="as-dom-pdf-form"><button class="button button-primary" type="submit" name="dom_generate_pdf" value="generate">Download</button></form>';
    echo '</div>';
}

this in turns calls this previously hooked function (also in my main plugin php file):

function rt_dom_generate_pdf() {
    if( isset($_POST['dom_generate_pdf'])){
        rt_dom_output_product_pdf();
    }
}
add_action( 'init', 'rt_dom_generate_pdf' );

which finally calls my actual function to make my pdf:

function rt_dom_output_product_pdf() {

    $options = new Options();
    $options->set('A4','potrait');
    $options->set('enable_css_float',true);
    $options->set('isHtml5ParserEnabled', false);
    $options->set('isRemoteEnabled', true);

    $html = '';
  
    include( 'rt_dom_product_detail.php');
        
    $dompdf = new DOMPDF($options);
    $html .= '<link type="text/css" media="dompdf" href="'.get_stylesheet_directory_uri().'/style.css" rel="stylesheet" />';
    $dompdf->loadHtml($html);

    $dompdf->render();
   
    $dompdf->stream('theProduct.pdf');
    exit;
}

This function includes a file called rt_dom_product_detail.php

The content of this file is:

<?php

global $wp;
global $wp_query;
global $post;

$post_id=$wp_query->get_queried_object_id();
$post_idnew=get_queried_object_id();
$queried_object = get_queried_object();
$get_the_id = get_the_id();

function get_the_post_id() {
  if (in_the_loop()) {
       $mypost_id = get_the_ID();
  } else {
       global $wp_query;
       $mypost_id = $wp_query->get_queried_object_id();
         }
  return $mypost_id;
} 

ob_start();
 
    $html .= '<h2>'.get_the_title($queried_object->id).'</h2>';
    $html .= 'queried object id is '.get_queried_object_id().'<br>';
    $html .= 'get the id is: '.get_the_id().'<br>';
    $html .= 'post_ID is: ' .$post_id.'<br>';
    $html .= 'post_IDnew is: ' .$post_idnew.'<br>';
    $html .= '$post->ID is: '.$post->ID.'<br>';
    $html .= 'mypost_id is: '.$mypost_id.'<br>';

get_the_title();
?>
Hi there 

<?php

$html .= ob_get_contents();
ob_end_clean();

No matter what I try (you can see I tried several options to get current Post ID (so i can pull title and custom fields) I don't get the post id.

The pdf is created and the contents are:

queried object id is 0
get the id is:
post_ID is: 0
post_IDnew is: 0
$post->ID is:
mypost_id is:
Hi there

How can I get the post id so I can print the title and custom fields?

I'm trying to create a plugin that will print the current custom post type for the currently viewed post type.

the idea is that if a user visits one of my custom post types (products) they will be able to click a button on that "page" and it will use DomPDF to print the page.

All my code is in a plugin except for the call for the funtion which is in myCHILDtheme/single-rt_products.php.

The button function is placed in my single-rt_products.php:

rt_dom_create_download_button();

this calls a function in my plugin main php file:

function rt_dom_create_download_button() {
    echo '<div class="wrap">';
    echo '<form method="post" id="as-dom-pdf-form"><button class="button button-primary" type="submit" name="dom_generate_pdf" value="generate">Download</button></form>';
    echo '</div>';
}

this in turns calls this previously hooked function (also in my main plugin php file):

function rt_dom_generate_pdf() {
    if( isset($_POST['dom_generate_pdf'])){
        rt_dom_output_product_pdf();
    }
}
add_action( 'init', 'rt_dom_generate_pdf' );

which finally calls my actual function to make my pdf:

function rt_dom_output_product_pdf() {

    $options = new Options();
    $options->set('A4','potrait');
    $options->set('enable_css_float',true);
    $options->set('isHtml5ParserEnabled', false);
    $options->set('isRemoteEnabled', true);

    $html = '';
  
    include( 'rt_dom_product_detail.php');
        
    $dompdf = new DOMPDF($options);
    $html .= '<link type="text/css" media="dompdf" href="'.get_stylesheet_directory_uri().'/style.css" rel="stylesheet" />';
    $dompdf->loadHtml($html);

    $dompdf->render();
   
    $dompdf->stream('theProduct.pdf');
    exit;
}

This function includes a file called rt_dom_product_detail.php

The content of this file is:

<?php

global $wp;
global $wp_query;
global $post;

$post_id=$wp_query->get_queried_object_id();
$post_idnew=get_queried_object_id();
$queried_object = get_queried_object();
$get_the_id = get_the_id();

function get_the_post_id() {
  if (in_the_loop()) {
       $mypost_id = get_the_ID();
  } else {
       global $wp_query;
       $mypost_id = $wp_query->get_queried_object_id();
         }
  return $mypost_id;
} 

ob_start();
 
    $html .= '<h2>'.get_the_title($queried_object->id).'</h2>';
    $html .= 'queried object id is '.get_queried_object_id().'<br>';
    $html .= 'get the id is: '.get_the_id().'<br>';
    $html .= 'post_ID is: ' .$post_id.'<br>';
    $html .= 'post_IDnew is: ' .$post_idnew.'<br>';
    $html .= '$post->ID is: '.$post->ID.'<br>';
    $html .= 'mypost_id is: '.$mypost_id.'<br>';

get_the_title();
?>
Hi there 

<?php

$html .= ob_get_contents();
ob_end_clean();

No matter what I try (you can see I tried several options to get current Post ID (so i can pull title and custom fields) I don't get the post id.

The pdf is created and the contents are:

queried object id is 0
get the id is:
post_ID is: 0
post_IDnew is: 0
$post->ID is:
mypost_id is:
Hi there

How can I get the post id so I can print the title and custom fields?

Share Improve this question edited Jun 1, 2024 at 5:46 rudtek asked Jun 1, 2024 at 5:25 rudtekrudtek 6,3535 gold badges30 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

That's not working for you because you've hooked the dompdf generation to init, which is too early for $post to be populated. Hook to something later like template_redirect instead.

本文标签: How Can I Retrieve post ID in plugin file