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

后端 未结 4 1010
既然无缘
既然无缘 2020-12-22 06:07

I know this is a question asked million times but I can\'t find an answer that is either fitting the version of Wordpress now and/or my particular case.

I have a Wor

4条回答
  •  自闭症患者
    2020-12-22 06:23

    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(); 
    }
    

提交回复
热议问题