基于OTP的登录注册插件出现ERR_TOO_MANY_REDIRECTS重定向过多问题
问题原因
出现ERR_TOO_MANY_REDIRECTS是因为重定向逻辑陷入了死循环:当用户被跳转到OTP页面后,代码依然会触发重定向判断——此时用户仍处于未验证状态且非管理员,所以会不断重复执行跳转到OTP页面的操作,形成无限循环。
修复后的代码
修改函数,增加「当前页面不是OTP页面」的判断,避免在OTP页面上触发重定向:
function my_redirect_handle_fun() { if (is_user_logged_in()) { $user_id = get_current_user_id(); $is_admin = current_user_can('administrator'); $verified = get_user_meta($user_id, "verified", true); // 获取当前页面的slug,用于判断是否在OTP页面 $current_page = get_query_var('pagename'); // 仅当非管理员、未验证、且不在OTP页面时,才执行重定向 if (!$is_admin && !$verified && $current_page !== 'otp') { error_log("Redirecting to OTP page."); wp_redirect(site_url("otp")); exit(); } } }
关键修改说明
- 新增
$current_page = get_query_var('pagename');获取当前页面的slug标识 - 在判断条件中加入
$current_page !== 'otp',确保用户处于OTP页面时不会被重复重定向
额外检查项
- 确认你的OTP页面slug确实是
otp,如果页面自定义了slug,需要替换成对应的值 - 确保函数挂载到正确的WordPress钩子上,比如常用的
template_redirect:add_action('template_redirect', 'my_redirect_handle_fun');
内容的提问来源于stack exchange,提问作者krish
相关产品推荐
相关产品推荐

