【问题标题】:Wordpress Database connection from FPDF来自 FPDF 的 Wordpress 数据库连接
【发布时间】:2018-09-11 12:16:24
【问题描述】:

我正在使用 tuto 构建一个小插件,以将数据库中的结果打印为 pdf。 我使用 FPDF 库来做。

现在我只想将用户名打印到我的 pdf 中。但它似乎对我不起作用......这是问题所在: “未捕获的错误:调用未定义的函数 wp_get_current_user()”

这是我的代码:

     <?php

           if ( ! defined( 'ABSPATH' ) ) {
            exit;
        }

include($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); } 


        include( 'atomicsmash-pdf-helper-functions.php');


        if( isset($_POST['generate_posts_pdf'])){
            output_pdf();
        }

        function output_pdf() {
          global $wpdb;
          $current_user = wp_get_current_user();
              $pdf = new PDF_HTML();
              $pdf->AddPage();
              $pdf->SetFont('Arial','B',16);
              $pdf->Cell(40,10, $current_user->display_name);
              $pdf->Cell(0,5,'L NAME:'.$current_user['display_name'], 0, 0, 'L');
              $pdf->Cell(40,10, 'Hello World');


            $pdf->Output('D','resultat.pdf');
            exit;
        }


        function as_fpdf_create_admin_page() {
        ?>
        <div class="wrap">
            <form method="post" id="as-fdpf-form">
                  <button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from Wordpress Posts</button>
            </form>
        </div>
        <?php
        }

好像我没有连接到 wpdb...

如果有人可以帮助我,我将非常感激!

再见

【问题讨论】:

    标签: wordpress fpdf


    【解决方案1】:

    我认为你需要包含 wp-load.php

    这是用于设置 ABSPATH 常量并加载 wp-config.php 文件的引导文件。然后 wp-config.php 文件将加载 wp-settings.php 文件,然后设置 WordPress 环境。

    如果你在插件文件夹中,那么你需要包括:

    require_once('../../../wp-load.php'); 
    

    【讨论】:

    • 感谢您的帮助 Yagnesh。但是我现在收到了这条消息:警告:require_once(../../../wp-load.php): failed to open stream: No such file or directory in ...
    • 我尝试了不同的方法,这个方法不会返回 wp-load 错误: 但是我总是从一开始就发现错误:call to undefined function wp_get_current_user()
    • 这应该可以解决您的问题。请在 wp-load.php
    • 很好用!!!非常感谢亚格内什。我现在将更新我的代码。你能解释一下什么是'pluggable.php'吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    相关资源
    最近更新 更多