将 Wordpress Core 包含到自己的脚本中 [英] Include Wordpress Core into own Scripts

查看:22
本文介绍了将 Wordpress Core 包含到自己的脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Wordpress 核心导入"到自己的脚本中以使用 wp_query 等功能.我在子目录(自己的框架)中创建了一个脚本并希望通过 wordpress 扩展它,但每次脚本抛出错误:

I'm trying to "Import" the Wordpress core into an own script to use the functionality such as wp_query etc. I've created an script in a subdirectory (own framework) and want to extend it by wordpress, but everytime the script throws an error:

致命错误:在 .../wordpress/wp-includes/taxonomy.php 线上的非对象上调用成员函数 add_rewrite_tag()b>333

Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/taxonomy.php on line 333

例如(当我删除 add_action('init', 'create_initial_taxonomies', 0 ) 时):

such as (when I remove the add_action( 'init', 'create_initial_taxonomies', 0 )):

致命错误:在 .../wordpress/wp-includes/post.php 线上的非对象上调用成员函数 add_rewrite_tag()b>1006

Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wordpress/wp-includes/post.php on line 1006

非对象是 $wp_rewrite-object.我已经回显了一些东西,并发现第一个 $wp_rewrite 是有效的,而在下一次调用时则无效.我对 WP 核心文件没有任何更改.

The non-object is the $wp_rewrite-object. I've echo'ed something and figured out that first $wp_rewrite is valid and at the next call not. I've changed nothing at the WP core files.

我尝试通过调用来包含核心:

I try to include the core by calling:

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

有人对我有什么想法吗?

Has anybody some ideas for me?

谢谢

推荐答案

简短的回答,这样做:

define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

答案很长,这是一个关于使用 PHP 导入脚本的微妙问题.

Long answer, it's a subtle gotcha around importing scripts with PHP.

如果您在所有函数之外定义了一个局部变量,则可以使用global"在函数内部检索它.如果函数中有一个局部变量,以后就不能使用 global 检索它,除非它当时被定义为全局变量.

If you define a local variable, outside of all functions, then it can be retrieved inside a function using 'global'. If you have a local variable inside a function, it cannot be retrieved later using global, unless it is defined as being global there and then.

脚本wp-settings.php"是问题所在.它是通过您调用包含 'wp-load.php' 来包含的.

The script 'wp-settings.php' is where the issue lies. It is included via your call to include 'wp-load.php'.

那里定义的变量没有声明为全局变量;相反,这是假定的,因为脚本总是在任何函数之外运行,因此是自动全局的.即

The variables defined there are not stated as being global; instead this is presumed because the script is always run outside of any functions, and so are automatically global. i.e.

$wordpress = 'foo';

function wordpressFunction() {
    global $wordpress;
}

因为您是在函数中导入脚本,所以它们现在变成了局部变量.您基本上是在做:

Because you are importing the script within a function, they now become local variables. You are essentially doing:

function myFramework() {
    $wordpress = 'foo';

    function wordpressFunction() {
        global $wordpress;
    }
}

所以解决方法是在导入脚本之前自己将它们定义为全局.现在可以正确找到 $wp_query 和其他定义为全局的.

So the fix is to define them as global yourself before importing the script. Now $wp_query, and the others defined as global, are correctly found.

这篇关于将 Wordpress Core 包含到自己的脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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