wp_redirect和标题重定向阻止我使用Wordpress功能


wp_redirect & header re-directs prevent me from using Wordpress functions

我的函数.php文件中的任何函数在我执行重定向后都不起作用。

更具体地说,我所做的是使用第一个 php 文件进行初始 API 调用,将数组写入 txt 文件,然后重定向到另一个 php 文件,该文件首先读取 txt 文件,然后基于第一个数组进行另一个 API 调用。然后解析该数据,并将其以特定格式放入 wp 数据库中。

我知道问题出在重定向上,因为我已经测试了多种场景,如下所示,它们都指向重定向是罪魁祸首。

例如,在我的第一个php文件中,我有以下内容。

if (function_exists('is_wp_version')){
    echo "is_wp_version exists!! <br>";
} else {
    echo "is_wp_version, it does not exist <br>";
}

if (function_exists('fopen')){
    echo "fopen exists!! <br>";
} else {
    echo "fopen, it does not exist <br>";
}
... More code...
... More code...
wp_redirect( 'PHPfile2.php', 301 ); exit; // I have also tried the header 
//function with same results
?>

如果我注释掉重定向行,我将始终看到打印的以下输出.

is_wp_version exists!!
fopen exists!!

但是,在 PHPfile2.php 中,我总是看到以下内容并得到一些错误和以下输出。

is_wp_version, it does not exist
fopen exists!!
Fatal error: Call to undefined function get_header() in PHPfile2.php on line 22

它给了我某种致命错误。 如果我使用 get_header 函数删除或注释掉代码,那么我在下一个 wordpress 函数上会出现致命错误。

有人可以指出我正确的方向吗? 我这样做的方式是错误的吗? 我只需要能够对第二个进行 2 次 API 调用,具体取决于我从第一个调用中得到的内容。

谢谢!

这是因为您离开了wordpress框架并使用自己的自定义文件(这是非常不可取的(。

你需要require_once('wp-blog-header.php');. 在PHPfile2.php.