Gideon Sassoon
Gideon Sassoon

Reputation: 604

Redirect to front page wordpress if not logged in

I'm looking to redirect a user to a specific page if they are not logged in. So blog.com/welcome

So they can either be viewing this page or wordpress login pages. I was trying to modify this plugin http://wordpress.org/plugins/redirect-to-login-if-not-logged-in/

add_action( 'wp', 'dmk_not_loggedin_redirect' );

function dmk_not_loggedin_redirect() {
    global $pagenow;

However I am ending up with redirect loops. And I could use some help to stop this.

if ( ! is_user_logged_in() && $pagenow != 'wp-login.php' or 'http://www.blog.co.uk/front' )
    wp_redirect( 'http://www.blog.co.uk/front' , 302 ); exit; }

Upvotes: 2

Views: 5810

Answers (1)

brasofilo
brasofilo

Reputation: 26075

I tried with the action hook init but the current post/page information is not available yet. But then, template_redirect does the job and it only acts on the front-end, login and admin pages work normally.

Here, there's a page with the slug welcome, to check for a post use is_single('post-slug'):

add_action( 'template_redirect', 'redirect_so_18688269' );

function redirect_so_18688269() 
{
    if( !is_user_logged_in() && !is_page( 'welcome' ) )
    {
        wp_redirect( site_url( 'welcome' ) );
        exit();
    }
}

Upvotes: 2

Related Questions