如何在自定义 .php 文件中包含 WordPress 功能? [英] How to include WordPress functions in custom .php file?

查看:34
本文介绍了如何在自定义 .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_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 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆