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

时间:2019-01-10 14:04:35

标签: php sql wordpress pdf

我想知道是否有人可以帮助我。我正在尝试根据来自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);
  }
}

当我在$post->ID中打印my_function_with_wc_functions()时,我可以看到正在输出的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

1 个答案:

答案 0 :(得分:1)

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

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

  2. 在表格中添加帖子ID。当前,它发送的唯一数据是您的提交按钮的名称。通过名为post的隐藏字段添加当前帖子的ID,然后您的脚本即可使用。