无法将 $post-ID 传递到 fpdb 函数 wordpress 管理屏幕



我想知道是否有人可以帮助我。我正在尝试根据wordpress数据库中的SQL查询生成PDF。查询必须是动态的,并且仅在 Wordpress 管理屏幕中显示基于当前页面的 $post->ID 的结果。

我正在使用 FPDB PDF 库并创建了一个函数output_pdf,我在其中尝试将 $post->ID 作为参数传递。

但是,当我尝试运行它时,我不断收到未定义的对象错误。这很令人沮丧,因为当我手动将 ID 添加为参数时,它的工作方式是这样的

add_action( 'woocommerce_init', 'my_function_with_wc_functions' );
  function my_function_with_wc_functions() {
  if( isset($_POST['generate_posts_pdf'])) {
    output_pdf(72);
  }
}

当我在my_function_with_wc_functions()中打印$post->ID时,我可以看到正在输出的 ID,所以我不确定为什么没有将其传递到 output_pdf 函数中。

谁能帮忙?

$pdf = new PDF_HTML();

add_action( 'woocommerce_init', 'my_function_with_wc_functions' );
  function my_function_with_wc_functions() {
  global $post;
  $post = $post ? $post : get_post($_GET['post']);
  if( isset($_POST['generate_posts_pdf'])) {
    output_pdf($post->ID);
  }
}
add_action('add_meta_boxes', 'as_fpdf_add_custom_box');
function as_fpdf_add_custom_box()
{
    $screens = ['product'];
    foreach ($screens as $screen) {
        add_meta_box(
            'as_fpdf_box_id',           // Unique ID
            'Entry List',  // Box title
            'as_fpdf_create_admin_page',  // Content callback, must be of type callable
            $screen,                   // Post type
            'normal'                  // Priority
        );
    }
}
add_action( 'admin_menu', 'as_fpdf_create_admin_menu' );
function as_fpdf_create_admin_menu() {
    $hook = add_submenu_page(
        'tools.php',
        'Atomic Smash PDF Generator',
        'Atomic Smash PDF Generator',
        'manage_options',
        'as-fdpf-tutorial',
        'as_fpdf_create_admin_page'
    );
}
function output_pdf($id) {
    global $wpdb;
      if ( $wpdb->get_var( $wpdb->prepare( 'SELECT lottery_id FROM ' . $wpdb->prefix . 'wc_lottery_log WHERE lottery_id= %d', $id ) ) ) {
      $log = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'wc_lottery_log WHERE lottery_id=%d', $id ) );
      global $pdf;
      $title_line_height = 10;
      $content_line_height = 8;
      $pdf->AddPage();
      $pdf->SetFont( 'Arial', '', 42 );
      $pdf->Write(20, 'Atomic Smash FPDF Tutorial');
      foreach( $log as $row ) {
          $pdf->SetFont( 'Arial', '', 22 );
          $pdf->Write($title_line_height, 'Test');
          // Add a line break
          $pdf->Ln(15);
          // Post Content
          $pdf->Ln(10);
          $pdf->SetFont( 'Arial', '', 12 );
          $pdf->WriteHTML($row->ticketid);
        }
    }
    $pdf->Output('D','atomic_smash_fpdf_tutorial.pdf');
    exit;
}
function as_fpdf_create_admin_page() {
?>
<div class="wrap">
    <h1>Generate Entry List</h1>
    <p>Click below to generate a pdf all all lottery entries. </p>
      <form method="post" id="as-fdpf-form">
        <button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from Competiion Entries</button>
    </form>
</div>
<?php
}

编辑

add_action( 'woocommerce_init', 'my_function_with_wc_functions' );
  function my_function_with_wc_functions() {
  global $post;
  $post = $post ? $post : get_post($_GET['post']);
  $id = $post->ID;
  print_r($post);
  if( isset($_POST['generate_posts_pdf'])) {
    output_pdf($id);
  }
}

返回

WP_Post Object
(
    [ID] => 72
    [post_author] => 1
    [post_date] => 2019-01-09 13:36:07
    [post_date_gmt] => 2019-01-09 13:36:07
    [post_content] => 
    [post_title] => Test5
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => open
    [ping_status] => closed
    [post_password] => 
    [post_name] => test5-2
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2019-01-10 13:46:08
    [post_modified_gmt] => 2019-01-10 13:46:08
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => /?post_type=product&#038;p=72
    [menu_order] => 0
    [post_type] => product
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

运行 output_pdf() 时打印错误

Notice: Undefined index: post in /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php on line 23
Notice: Trying to get property 'ID' of non-object in /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php on line 24
Notice: Undefined variable: pdf in /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php on line 85
Fatal error: Uncaught Error: Call to a member function Output() on null in /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php:85
Stack trace:
#0 /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php(28): output_pdf(NULL)
#1 /app/public/wp-includes/class-wp-hook.php(286): my_function_with_wc_functions('')
#2 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#3 /app/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#4 /app/public/wp-content/plugins/woocommerce/includes/class-woocommerce.php(503): do_action('woocommerce_ini...')
#5 /app/public/wp-includes/class-wp-hook.php(286): WooCommerce->init('')
#6 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#7 /app/public/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#8 /app/public/wp-settings.php(467): do_action('init')
#9 /app/public/wp-config.php(76): require_once('/app/public/wp-...')
#10 /app/public/wp-load.php(37): require_o in /app/public/wp-content/plugins/atomicsmash-pdf-tutorial/atomicsmash-pdf-tutorial.php on line 85

正如 PHP 通知所说,$_GET['post']确实没有设置。表单通过 $_POST 发送数据,因此为了使脚本正常工作,您需要:

  1. $_GET['post']更改为$_POST['post']

  2. 在表单中包含帖子 ID。目前,它发送的唯一数据是提交按钮的名称。通过名为 post 的隐藏字段包含当前帖子的 ID,然后您的脚本将起作用。

最新更新