没有人访问我的完整的wordpress网站没有登录,如果用户没有丢失或重定向到http://example/submit-project/。
我试着用下面的代码来完成这个任务:
$current_user = wp_get_current_user(); $crntusr = $current_user; if($crntusr->ID == 0){ wp_redirect( 'example.com/login'; ); }但是得到这个错误:
警告:无法修改已由/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-content/themes/freelanceengine/header.php:14)发送的标头信息(输出从第1195行的/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-includes/pluggable.php中的/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-includes/pluggable.php开始)
发布于 2017-04-29 07:35:27
1) wp_redirect()不会自动退出,而且几乎总是应该后面有一个退出调用。
2)在模板输出之前,您应该进行重定向。
3)最好不要使用http://example.com这样的绝对链接,您可以通过wp_login_url()函数获得WP登录页面。
从header.php文件中删除重定向代码,并尝试将此代码添加到functions.php中:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) ) {
wp_redirect(wp_login_url());
exit;
}
} 更新。如果您的登录表单位于自定义页面(http://example/submit-project/)上,则应使用以下代码:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and $_SERVER['REQUEST_URI'] != '/submit-project/' ) {
wp_redirect('http://example/submit-project/');
exit;
}
}https://stackoverflow.com/questions/43692969
复制相似问题