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

查看:108
本文介绍了将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:

致命错误:在中的非对象上调用成员函数add_rewrite_tag() ./wordpress/wp-includes/taxonomy.php 在线 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 )):

致命错误:调用成员函数add_rewrite_tag() ... / wordpress / wp-includes / post.php 中的非对象 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'.

其中定义的变量 not 表示为全局变量;相反,这是假定的,因为脚本总是在任何函数之外运行,因此自动全局。即。

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天全站免登陆