帮助无法解决此问题。
我构建了一个基于this tutorial 这使用户能够使用FPDF将自定义帖子类型的帖子导出到格式化的PDF中。
目前,PDF导出工作正常,但当前帖子没有传递给插件函数。一个按钮通过自定义快捷码添加到帖子中。单击按钮时,它应该会自动获取当前查看的帖子并生成内容的PDF。我可以在函数中输入一个特定的帖子ID,它会按预期导出该特定帖子。如果我只使用没有参数的get\\u post,它会导出ID=1的已被丢弃的post的PDF,而不管shortcode按钮位于哪个post中。
仅供参考,这些帖子是通过AJAX加载的,所以问题是否与此有关?
我做错了什么?!有没有更好的方法?我感谢你的帮助!
lsmg-pdf.php
<?php
/*
* Plugin Name: [redacted]
* Description: A plugin created to apply PDF export functionality to documents.
* Version: 1.0
* Author: [redacted]
*/
if ( ! defined( \'ABSPATH\' ) ) {
exit;
}
include( \'lsmg-pdf-helper-functions.php\');
$pdf = new PDF_HTML();
if( isset($_POST[\'generate_posts_pdf\'])){
output_pdf();
}
function output_pdf() {
$post = get_post();
global $pdf;
$title_line_height = 10;
$content_line_height = 8;
$pdf->AddFont( \'Lato\', \'\', \'Lato-Regular.php\' );
$pdf->AddFont( \'Lato\', \'B\', \'Lato-Bold.php\' );
$pdf->AddFont( \'Lato\', \'I\', \'Lato-Italic.php\' );
$pdf->SetMargins(12.7, 12.7);
$pdf->SetDrawColor(36,161,89);
$pdf->AddPage();
$pdf->SetFont( \'Lato\', \'B\', 24 );
$pdf->SetTextColor(4,23,51);
$pdf->Write($title_line_height, $post->post_title);
$pdf->SetLineWidth(1);
$pdf->Line(14, 27, 40, 27);
// Add a line break
$pdf->Ln(14);
// Image
$page_width = $pdf->GetPageWidth() - 20;
$max_image_width = $page_width;
$image = get_the_post_thumbnail_url( $post->ID );
if( ! empty( $image ) ) {
$pdf->Image( $image, null, null, 100 );
}
// Post Content
$pdf->Ln(10);
$pdf->SetFont( \'Lato\', \'\', 10 );
$pdf->SetTextColor(38,59,71);
$pdf->WriteHTML($post->post_content);
$pdf->Output(\'D\',\'lsmg-guideline-doc.pdf\');
exit;
}
function lsmg_pdf() {
?>
<div class="wrap">
<form method="post" id="as-fdpf-form">
<button class="custom-botton" type="submit" name="generate_posts_pdf" value="generate">Download PDF</button>
</form>
</div>
<?php
}
function register_shortcodes(){
add_shortcode(\'fpdf-doc\', \'lsmg_pdf\');
}
add_action( \'init\', \'register_shortcodes\');