简体   繁体   中英

Wordpress: Login for Users Without access to wp-admin

I have a wordpress application and I want the public to be able to view the website only when they are registered and logged-in.

Also, I don't want users to have access to wp-admin when they signup. I used Theme My Login plugin, but all it does is help you get a customized wp-login page, because registered users will get access to wp-admin.

PS: I am sorry if this question have been asked before, I could not find it, it is so hard to google "wordpress with custom login" without seeing a page that just customizes the wp-login page.

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!

强制登录 WordPress插件可能是您正在寻找的东西。

In my opinion I would still allow users to access the dashboard so they can use profile section to change their password, email etc. At basic, subscriber level they cannot do much more so you should not be worried.

If you still insist on prohibiting them from accessing the dashboard this might come helpful https://premium.wpmudev.org/blog/limit-access-to-your-wordpress-dashboard/

Here is one scenario which make sense, registered users will have role of subscriber editor or whatever you intended to set. you need to set restrictions for registered users those will not be able to access wp-admin

here is the code which can help you to achieve that.

you need to replace USER_ROLE_NAME_HERE it with role of register user.

function wp_register_user_no_admin_access(){
    $redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : home_url( '/' );
    if ( current_user_can( 'USER_ROLE_NAME_HERE' ) ){
        exit( wp_redirect( $redirect ) );
    }
}
add_action( 'admin_init', 'wp_register_user_no_admin_access', 100 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM