我无法在自定义wordpress插件中获得重定向。我在页面上有一个按钮,可以触发调用函数的AJAX操作。
在我的函数中(当我在调试日志中看到“用户未登录”的输出时,肯定会调用该函数),我有:
error_log("User is not logged in!");
wp_redirect(home_url());
exit();
我尝试过的事情:硬编码url,尝试其他url,使用exit();并退出,使用header()我不知道为什么这不起作用。每个其他类似问题的答案都是add exit();但这对我来说并不奏效。谢谢
编辑:
调整了AJAX回调,如下所示:
<script>
// Create a function to pick up the link click
jQuery(document).ready(function($) {
$(\'.button\').click(function(e){
e.preventDefault();
console.log("Hi")
jQuery.post(
game_mode_register.ajax_url,
{
\'action\': \'game-mode-register\',
\'type\': $(this).attr("value")
}.fail(function(response){
console.log("ajax response");
//window.location.href = response.redirect_url
});
);
});
});
参数列表后的错误输出:SyntaxError:缺失),引用//窗口后面的行。地方
此外,我的wp\\u重定向似乎被拾取但被阻止。Javascript控制台显示:
阻止加载混合活动内容“http://my_website.com/”
我有一个最新的SSL证书。也不知道这意味着什么。
编辑:
从第一个答案给出的方向来看,失败的尝试包括:
wp_send_json_success( $data );
wp_send_json_error( $data );
wp_send_json( $data );
echo json_encode($data);