如何在自定义 .php 文件中包含 WordPress 功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15304926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include WordPress functions in custom .php file?
提问by Sziro
How can I include WordPress functions in a custom .php file?
如何在自定义 .php 文件中包含 WordPress 功能?
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:
详细说明:我的主题(构造函数)下有一个名为报告的目录。这些包含 .php 文件,这些文件从带有 DOMPDF 的站点数据生成报告以供下载。对于这些,我想使用 WordPress 引擎提供的功能,例如get_the_author_meta( 'user_firstname', $user_id ). 如果我使用这些,我会(自然地)出现以下错误:
Fatal error: Call to undefined function get_the_author_meta() in ROOT/public_html/wp-content/themes/constructor/reports/testreport.php on line 15
致命错误:在第 15 行调用 ROOT/public_html/wp-content/themes/constructor/reports/testreport.php 中未定义的函数 get_the_author_meta()
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:
我相信我需要包含 wp-blog-header.php 。我用require_once("../../../../wp-blog-header.php");. 有了这个,我得到以下 404 错误:
No webpage was found for the web address: ROOT/wp-content/themes/constructor/reports/testreport.php
没有找到该网址的网页:ROOT/wp-content/themes/constructor/reports/testreport.php
(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.)
(要求指向正确的路径。如果我摆弄它,我会收到警告:require_once(../../../wp-blog-header.php): failed to open stream... 所以路径必须正确。)
Is there something I overlook? Why can't I include this wp file? What is the correct method to include the wp functions?
有什么我忽略的吗?为什么我不能包含这个 wp 文件?包含 wp 功能的正确方法是什么?
Thanks for the help, Sziro
谢谢你的帮助,西罗
回答by seanbreeden
You're on the right track. Try this instead:
你在正确的轨道上。试试这个:
require_once("../../../../wp-load.php");
回答by Salam El-Banna
To use wp functions in custom .php files, you must include wp-load.phpin your file.
You can do so by adding the following line:
要在自定义 .php 文件中使用 wp 函数,您必须包含wp-load.php在您的文件中。您可以通过添加以下行来实现:
require_once(PATH_TO.'/wp-load.php');
If WordPress is in the document root add instead:
如果 WordPress 在文档根目录中,请改为添加:
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
回答by VST
Well if someone has newer PHP versions installed (ver >= 5.5.x) then they can also try the below code in the root script in WordPress website directory itself:
好吧,如果有人安装了较新的 PHP 版本(版本 >= 5.5.x),那么他们也可以在 WordPress 网站目录本身的根脚本中尝试以下代码:
<?php
define("WP_ROOT", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
require_once WP_ROOT . DS . "wp-load.php";
Or
或者
<?php
define("WP_ROOT", __DIR__);
define("DS", DIRECTORY_SEPARATOR);
require_once WP_ROOT . DS . "wp-blog-header.php";
I guess this is a more direct and clean approach and doesn't involve manually adding slashes and changing diretories by ...
我想这是一种更直接和干净的方法,不涉及手动添加斜杠和更改目录..。
Hope this helps someone.
希望这可以帮助某人。
回答by Syed Naeem Tariq
require_once(dirname(__FILE__) . '/options.php');
This is better way to include a file in WordPress.
这是在 WordPress 中包含文件的更好方法。
回答by Aman Dhanda
External files can easily access the WordPress functions. You just need to include the file wp-load.phpin your external file.
The wp-load.phpfile is located in root of your WordPress installation.
Example: Suppose your file is test.phplocated at root directory of WordPress installation.
外部文件可以轻松访问 WordPress 功能。您只需要将该文件包含wp-load.php在您的外部文件中。该wp-load.php文件位于 WordPress 安装的根目录中。示例:假设您的文件test.php位于 WordPress 安装的根目录。
<?php
require_once('wp-load.php');
// Your custom code
?>
回答by Maidul
I use this method to load WordPress environment outside WordPress.
我使用这种方法在 WordPress 之外加载 WordPress 环境。
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php')) {
/** Loads the WordPress Environment and Template */
require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
}

