How to include WordPress functions in custom .php file?(如何在自定义 .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");
这篇关于如何在自定义 .php 文件中包含 WordPress 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在自定义 .php 文件中包含 WordPress 函数?


基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01