How to redirect non logged in users to Login page on Wordpress?

二次信任 提交于 2019-11-29 18:09:43
Daniel C

There are a lot of different ways to do this based on what your ultimate goal is (use WP login page, a custom login page, etc...). You can try adding this to your theme's functions.php file:

if ( ( is_single() || is_front_page() || is_page() || is_archive() || is_tax() )
    && ! is_page( 'login' ) && ! is_page('register') && ! is_user_logged_in() ) {
    auth_redirect(); 
}

Or you can use the plugin, Force Login

UPDATE

Theoretically, you can probably just use this, just haven't tested...

if( ! is_page('login') && ! is_page('register') && ! is_user_logged_in() ) {
    auth_redirect(); 
}

Take a look here

https://wordpress.org/support/topic/how-to-auth_redirect-to-specific-page

<?php
if( !is_user_logged_in() ) {
    wp_redirect('http://somepagehere');
    exit;
}
?>

You can also add this to your functions.php file instead of what I said before:

function admin_redirect() {
if ( !is_user_logged_in()) {
   wp_redirect( home_url('/{custom page goes here}/') );
   exit;
}
}
add_action('get_header', 'admin_redirect');

Just use this wordpress plugin https://wordpress.org/plugins/restrict-user-access/ to control who can/ can't access your website.

You may get tons of similar plugins if you googled it.

What you want it to check if the user is not currently trying to login or register, if not redirect them to whatever page you'd like.

if ( !is_user_logged_in() && !is_page( 'login' ) && ! is_page('register') ) {
   //redirect user, create account with them, do a hoola-hoop
}

Be sure to place this code in your page file before get_header();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!