【问题标题】:Wordpress Plugin template_redirect throws 404Wordpress 插件 template_redirect 抛出 404
【发布时间】:2013-04-01 02:26:27
【问题描述】:

我正在尝试使用 template_redirect 通过 Wordpress 中的 URL 加载插件的特定文件。文件显示正确,但发送的标头是 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 吗?

【问题讨论】:

  • 您是否尝试过先检查404,然后删除标志并添加header("HTTP/1.1 200 OK");?

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


【解决方案1】:

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

要对抗 404 状态,只需对其进行测试,删除标志,然后使用包含发送标头 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2013-09-20
    • 1970-01-01
    • 2018-04-26
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多