WordPress: Login for Users Without access to wp-admin

I had a WordPress site requirement where only logged in users could see the site and it’s contents. I came across this bit of code a few years ago and found it to be helpful. Place it into your functions.php and when a visitor comes along who is not logged in, it’ll redirect them to the wp-login.php page to login. Once logged in, then they can view the site without being blocked.

This is an example where it can help keep unwanted visitors away from the content of your site.

Add the block below to make your website available to logged on authenticated users only. If a user is not authenticated, then they will be redirected to the login page

    <?php

    // Redirect users who arent logged in...
    function members_only() {
        global $pagenow;
        // Check to see if user in not logged in and not on the login page
        if( !is_user_logged_in() && $pagenow != 'wp-login.php' )
              auth_redirect();
    }
    add_action( 'wp', 'members_only' ); 

    ?>

And if you’re looking for a custom login page? I know there’s a plugin that can do that for you such as “Theme My Login”. Also, after a user logs in you want them to be redirected back to the site and no dashboard access? You could try the plugin “Remove Dashboard Access”. Hope this helps!

Leave a Comment

error code: 523