在我的 PHP 应用程序中访问 WordPress API

发布于 2024-12-12 00:13:31 字数 1235 浏览 0 评论 0原文

我从 PHP 代码中访问 WordPress API,仅包含 wp-blog-header.php 并使用 get_posts()。

当我的 PHP 代码与 WP 目录处于同一级别时,这可以正常工作。例如:

/blog/[..wp files..]
/index.php

在index.php,我有:

require('blog/wp-blog-header.php');
$post = get_posts(...);

并且工作得很好。

但是,当我尝试对目录内的代码执行相同操作时,例如:

/blog/[..wp files..]
/folder/index.php

在folder/index.php 中,我有:

require('../blog/wp-blog-header.php');
$post = get_posts(...);

这总是使我的应用程序重定向到 WP 安装(wp-admin/install.php)并且不起作用。

有什么想法吗?可以在目录中使用 API 吗?

该博客配置正确并且本身运行正常。


编辑:堆栈跟踪显示在文件夹内使用时应用程序何时重定向到 wp_not_installed()

6   0.4052  3402748 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-load.php' )   ../class_wordpress.php:11
7   0.4054  3408296 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-config.php' ) ../wp-load.php:30
8   0.4063  3487064 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-settings.php' )   ../wp-config.php:19
9   1.3650  6103276 wp_not_installed( ) ../wp-settings.php:100
10  1.6258  7676148 wp_redirect( )  ../load.php:408
11  1.6261  7684480 header ( )  ../pluggable.php:890

谢谢!

I'm accessing the WordPress API from within my PHP code just including the wp-blog-header.php and using get_posts().

This is working ok when my PHP code is at the same level than the WP directory. For example:

/blog/[..wp files..]
/index.php

At index.php, I have:

require('blog/wp-blog-header.php');
$post = get_posts(...);

and is working pretty well.

BUT, when I try to do the same with the code inside a directory, for example:

/blog/[..wp files..]
/folder/index.php

and at folder/index.php I have:

require('../blog/wp-blog-header.php');
$post = get_posts(...);

this always makes my app to redirect to the WP installation (wp-admin/install.php) and doesn't work.

Any idea ? Can the API be used from within a directory ?

The blog is configured correctly and is working ok by itself.


Edit: Stack trace showing when the app redirects to wp_not_installed() when used inside a folder

6   0.4052  3402748 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-load.php' )   ../class_wordpress.php:11
7   0.4054  3408296 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-config.php' ) ../wp-load.php:30
8   0.4063  3487064 require_once( '/usr/www/juanjo/NuevaWebJuanjo/blog/wp-settings.php' )   ../wp-config.php:19
9   1.3650  6103276 wp_not_installed( ) ../wp-settings.php:100
10  1.6258  7676148 wp_redirect( )  ../load.php:408
11  1.6261  7684480 header ( )  ../pluggable.php:890

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听风念你 2024-12-19 00:13:31

对于在 WordPress 系统外部访问的所有 WordPress,您必须包含 wp-load.php

include("/absolute/path/to/wp-load.php");
//do whatever you want

For everything wordpress that is accessed outside wordpress system, you HAVE to include wp-load.php

include("/absolute/path/to/wp-load.php");
//do whatever you want
假面具 2024-12-19 00:13:31

我认为@silent意味着你需要将两者都包含在内才能工作。然而,当我运行 WordPress 时,我需要两者。

I think @silent means that you need to have both included for it to work. I however need both when I'm running WordPress.

梦回旧景 2024-12-19 00:13:31

看看这个 http: //www.webopius.com/content/139/using-the-wordpress-api-from-pages-outside-of-wordpress

添加:

define('WP_USE_THEMES', false);

显然在之前

require('blog/wp-blog-header.php');

就可以了。

Take a look at this http://www.webopius.com/content/139/using-the-wordpress-api-from-pages-outside-of-wordpress

Apparently adding:

define('WP_USE_THEMES', false);

right before:

require('blog/wp-blog-header.php');

does the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文