在REST风格的API中包含WordPress

时间:2013-01-28 作者:ken

我正在使用一个很棒的小开源产品Restler 为Wordpress应用程序提供RESTful API。为了实现这一点,我希望能够将Wordpress环境加载到一组PHP类中,Restler将在收到请求时运行这些PHP类。

加载WP环境(不加载UI元素)的想法是通过一个名为AddWordpress的脚本实现的。php:

<?php
error_reporting(E_ALL);
$site_name=\'yoursite\';
$site_domain=\'www.yoursite.com\';

/**
* Construct a fake $_SERVER global to get WordPress to load a specific site.
* This avoids alot of messing about with switch_to_blog() and all its pitfalls.
*/
$_SERVER=array(
\'HTTP_HOST\'=>$site_domain,
\'REQUEST_METHOD\'=>\'GET\',
\'REQUEST_URI\'=>"/{$site_name}/",
\'SERVER_NAME\'=>$site_domain,
);

// Remove all our bespoke variables as they\'ll be in scope as globals and could affect WordPress
unset($site_name,$site_domain);

// Pretend that we\'re executing an AJAX process. This should help WordPress not load all of the things.
define(\'DOING_AJAX\',true);

// Stop WordPress doing any of its normal output handling.
define(\'WP_USE_THEMES\',false);

// turn on debugging
define(\'WP_DEBUG\', true);
define(\'WP_DEBUG_LOG\', true);

// Load WordPress - intentionally using an absolute URL due to issues with relative paths on the CLI.
include "/[path-to-root-www]/wp-load.php";
这很有魅力。我现在可以从命令行运行这样的脚本(php test.php 其中,下面的脚本是测试。php):

<?php
include "AddWordpress.php";
$terms = get_terms("actions");
echo json_encode($terms);
我遇到的问题是,如果我不想将“AddWordpress.php”包含在文件的顶部,而是包含在方法调用中——我知道这听起来是一个非常糟糕的主意,但有很好的理由。。。请相信我,然后所有的狗屎都散开了,我的日子很快就变得很糟糕。更具体地说,测试文件现在如下所示:

<?php
function do_that_wordpress_thing() {
    include "AddWordpress.php";
}
do_that_wordpress_thing();
$terms = get_terms("actions");
echo json_encode($terms);
我得到的错误如下:

Notice:  is_404 was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_home was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944
Notice:  is_archive was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.) in /[path-to-www-root]/wp-includes/functions.php on line 2944

... etc ...
* UPDATE *我在PHP上学到的东西。net是包含函数def上的以下代码段:

如果include发生在调用文件中的函数中,那么被调用文件中包含的所有代码的行为都将如同它是在该函数中定义的一样。因此,它将遵循该函数的可变范围。此规则的一个例外是在包含发生之前由解析器计算的神奇常量。

我想这一定是为什么这不起作用的一个重要原因,但我仍然无可救药地迷失了方向。有人能解释为什么会发生这种情况吗?任何帮助都将不胜感激。

1 个回复
最合适的回答,由SO网友:ken 整理而成

这个答案应该归功于@Wyck,他正确地确定了问题的根源。非常感谢你。出现问题的原因是:

主线代码中的导入语句与函数中的导入语句之间存在差异。在WP或任何插件中,如果变量未显式设置为全局,则它们将在本地作用于要导入的函数。

遗憾的是,没有import指令来显式地将变量定向到特定的名称空间,但如果没有这个指令,您可以简单地将所有全局变量声明为函数中的“全局”。使用与我最初的问题陈述中相同的演示文件,现在可以工作了:

<?php
function do_that_wordpress_thing() {
    global $domain, $path, $base, $admin_page_hooks, $ajax_results, $all_links, $allowedposttags, $allowedtags, $authordata, $bgcolor, $cache_categories, $cache_lastcommentmodified, $cache_lastpostdate, $cache_lastpostmodified, $cache_userdata, $category_cache, $class, $comment, $comment_cache, $comment_count_cache, $commentdata, $current_user, $day, $debug, $descriptions, $error, $feeds, $id, $is_apache, $is_IIS, $is_macIE, $is_winIE, $l10n, $locale, $link, $m, $map, $max_num_pages, $menu, $mode, $month, $month_abbrev, $monthnum, $more, $multipage, $names, $newday, $numpages, $page, $page_cache, $paged, $pagenow, $pages, $parent_file, $preview, $previousday, $previousweekday, $plugin_page, $post, $post_cache, $post_default_category, $post_default_title, $post_meta_cache, $postc, $postdata, $posts, $posts_per_page, $previousday, $request, $result, $richedit, $single, $submenu, $table_prefix, $targets, $timedifference, $timestart, $timeend, $updated_timestamp, $urls, $user_ID, $user_email, $user_identity, $user_level, $user_login, $user_pass_md5, $user_url, $weekday, $weekday_abbrev, $weekday_initial, $withcomments, $wp, $wp_broken_themes, $wp_db_version, $wp_did_header, $wp_did_template_redirect, $wp_file_description, $wp_filter, $wp_importers, $wp_plugins, $wp_taxonomies, $wp_the_query, $wp_themes, $wp_object_cache, $wp_query, $wp_queries, $wp_rewrite, $wp_roles, $wp_similiesreplace, $wp_smiliessearch, $wp_version, $wpcommentspopupfile, $wpcommentsjavascript, $wpdb;
    global $load_sections; // for pagelines theme
    include "AddWordpress.php";
}
do_that_wordpress_thing();
$terms = get_terms("actions"); // I have a custom taxonomy named "actions"
echo json_encode($terms);
同样值得注意的是,如果在插件中使用自动加载器;您应该使它们与命名空间兼容。例如,而不是:

spl_autoload_register (  \'autoloader_function_name\' );
使用此选项:

spl_autoload_register (  __NAMESPACE__ . \'\\\\autoloader_function_name\' );

结束

相关推荐

WordPress Ajax Problems

更新我使用以下curl命令启动它:curl -H \"Accept: application/json\" -X POST http://localhost/wordpress/wp-admin/admin-ajax.php -d \"action=vixo_wordpress_signon&data=banjometer\" --trace -不太确定如何让它接受json,但嘿。。。最初的问题是,我是WordPress的新手,在使用Ajax时遇到了一些问题,但在理解WordPress如何加载PH