如何在自定义 .php 文件中包含 WordPress 函数?

2022-01-25 00:00:00 include php wordpress

如何在自定义 .php 文件中包含 WordPress 函数?

详细说明:我的主题(构造函数)下有一个名为报告的目录.这些包含 .php 文件,这些文件使用 DOMPDF 从站点的数据生成报告以供下载.对于这些,我想使用 WordPress 引擎提供的函数,例如 get_the_author_meta('user_firstname', $user_id).如果我使用这些,我(自然)会得到以下错误:

In detail: I have a directory under my theme (Constructor) named reports. These contain .php files that generate reports from data from the site with DOMPDF for downloading. For these I would like to use functions that the WordPress engine provides, for example get_the_author_meta( 'user_firstname', $user_id ). If I use these i get (naturally) the following error:

致命错误:在第 15 行的 ROOT/public_html/wp-content/themes/constructor/reports/testreport.php 中调用未定义函数 get_the_author_meta()

Fatal error: Call to undefined function get_the_author_meta() in ROOT/public_html/wp-content/themes/constructor/reports/testreport.php on line 15

我被引导相信我需要包含 wp-blog-header.php .我使用 require_once("../../../../wp-blog-header.php"); .有了这个,我得到以下 404 错误:

I was lead to believe that I need to include wp-blog-header.php . I use require_once("../../../../wp-blog-header.php"); . With this I get the following 404 error:

没有找到该网址的网页:ROOT/wp-content/themes/constructor/reports/testreport.php

No webpage was found for the web address: ROOT/wp-content/themes/constructor/reports/testreport.php

(require 指向正确的路径.如果我摆弄它,我会收到警告:require_once(../../../wp-blog-header.php): failed to open stream... 所以路径必须正确.)

(The require points to the correct path. If I fiddle with it, I get Warning: require_once(../../../wp-blog-header.php): failed to open stream... So the path must be correct.)

有什么我忽略的吗?为什么我不能包含这个 wp 文件?包含 wp 函数的正确方法是什么?

Is there something I overlook? Why can't I include this wp file? What is the correct method to include the wp functions?

谢谢你的帮助,西罗

推荐答案

你在正确的轨道上.试试这个:

You're on the right track. Try this instead:

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

相关文章