3

Is there a simple way to temporarily stop a user role logging in with wordpress?

For example, if I have a user role called media, how can I block them from logging in?

I would like a custom message to appear on the website, for example like 'Site undergoing maintenance'

So it looks like this:

https://i.stack.imgur.com/TgAia.jpg

The reason why, is because my website is completely locked down from the public using this function...

// LOCK DOWN
add_action('get_header', 'wpq_member_only_site');
function wpq_member_only_site() {
    // logged in users or visits to a specified page are allowed
    if ( !is_user_logged_in() ) {
        
        $redirect_after_login = get_home_url();
        
        // the URL where login/registration takes place
        $login_url = wp_login_url( $redirect_after_login );
                
        // redirect visitors
        wp_redirect( $login_url, 302 );
        exit;
    }
}

But I need to do some maintenance and I'm after a simple way to lock all users out that are the role media.

This is because I wan't administrators and editors to still have access, but not media.

3
  • I created a simple plugin that will allow you to do this without messing around with code. The message displayed to disabled users is also filterable. wordpress.org/plugins/disable-users
    – jaredatch
    Aug 29, 2013 at 2:17
  • If you want to use the ready-made plugin for a temporary block of certain user's accounts then the User Blocker WordPress plugin is a useful solution for you. https://wordpress.org/plugins/user-blocker/ Feb 11, 2021 at 5:31
  • 1
    I converted the above 2 answers into comments as they are not answers, they're recommendations. Answers must be self contained, you can use links to support an answer, but the link can't be the answer itself
    – Tom J Nowell
    Feb 11, 2021 at 9:41

3 Answers 3

5

With a some digging and learning, I managed to combine various help and create these 2 functions...

// MAINTAINANCE MODE
function site_maintenance() {
    if ( current_user_can('media') || current_user_can('genpo') ) {

       $logout_url = wp_login_url().'?mode=maintainance';
       wp_logout();
       wp_redirect( $logout_url, 302 );

    }     
}
add_action('get_header', 'site_maintenance');

// CUSTOM LOGIN MESSAGES
function my_login_message() {

    if( $_GET['mode'] == 'maintainance' ){
        $message = '<p class="message"><b>Site undergoing maintainance.</b></p>';
        return $message;
    }

}
add_filter('login_message', 'my_login_message');
1
3

Add in a check for a capability that admins and editors share:

add_action( 'get_header', 'wpse81659_maintenance' );
function wpse81659_maintenance() {
    if ( ! current_user_can( 'publish_pages' ) || ! is_user_logged_in() )
        die( 'Site undergoing maintenance' );
}
1
  • Thanks diggy. I will use this if no other solution comes in, but I would ideally like the message to appear here, so it looks like this... i.imgur.com/nDf7a.jpg - Thank you very much for your suggestion.
    – Joshc
    Jan 15, 2013 at 14:59
0
add_filter( 'authenticate', 'restrict_media_role_authenticate', 99 );
function restrict_media_role_authenticate( $user, $username, $password ){
    if ( in_array( 'media', (array) $user->roles ) ) {
        return new WP_Error( 'denied', "Site undergoing maintenance." );
    }
    return $user;
}
2
  • 1
    Welcome to wpse! what does this code supposed to do? how does it solve OPs problem?, consider adding explanation for this code, what it does and how it solves the problem, also, where to add it. As it stands now, this answer is not really helpful. Nov 24, 2021 at 13:35
  • Hey @Buttered_Toast , ty, in my understanding WP developers knows where to add filters: usually that is done in plugins (if needed) or theme functions.php file. That code deny media role users login with custom message, as stated in 3 lines of a problem description and further image.
    – p.voinov
    Dec 6, 2021 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.