php - Wordpress 插件 template_redirect 抛出 404

标签 php wordpress plugins http-status-code-404

我正在尝试使用 template_redirect 通过 Wordpress 中的 URL 加载插件的特定文件。文件显示正确,但发送的 header 是 404,而不是 200。

插件中的代码是:

add_filter('query_vars','plugin_add_jb');
function plugin_add_jb($vars) {
    $vars[] = 'jb_ajax';
    return $vars;
}

add_action('template_redirect', 'plugin_jb_check');
function plugin_jb_check() {
    if(intval(get_query_var('jb_ajax')) == '1') {
        $jb_action = $_GET['action'];
        if($jb_action == 'refer') {
            include('./wp-content/plugins/JobBounties/ajax_refer.php');
        }
        elseif ($jb_action == 'refer_post') {
            include('./wp-content/plugins/JobBounties/ajax_refer_post.php');
        }
        exit;
    }
}

有人知道为什么会抛出 404 吗?

最佳答案

Wordpress 可能无法识别此插件或数据并抛出 404 状态。

要对抗 404 状态,只需对其进行测试,删除标志,然后使用包含发送 header 200 状态。

function wp15905643_include($template) {
    global $wp_query;
    if ($wp_query->is_404) {
        $wp_query->is_404 = false;
    }
    header("HTTP/1.1 200 OK");
    include($template);

}

然后在你的 plugin_jb_check 函数中使用它,如果有帮助的话。

wp15905643_include('./wp-content/plugins/JobBounties/ajax_refer.php');

来源 here .

关于php - Wordpress 插件 template_redirect 抛出 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905643/

相关文章:

php - 发送 AJAX 到 PHP 不显示

WordPress,使用 cookie 进行类别重定向

css - 如何删除应用于 wordpress 仪表板上的 css 规则?

windows - 将可加载插件链接到 Linux 和 Windows 上可执行文件中的符号

php - "Change DB Prefix"wordpress 插件错误/问题 : redirecting all HTTP traffic to/wp-admin/install. php

php - PHP 中的对象比较

javascript - 将 Jquery 函数放在一个单独的文件中 - Joomla

maven - 为什么列出插件的目标而不绑定(bind)到阶段?

php - 如何在与 artisan 一起运行的 laravel 5 中包含 CSS?

php - 更改 Wordpress 仪表板 Logo (多站点)