Ask your WordPress questions! Pay money and get answers fast! (more info)

Block user-role login & show message (HALF SOLVED) WordPress

  • SOLVED


<strong>PLEASE SEE BOTTOM OF QUESTION AS I HALF SOLVED IT...</strong>


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... [[LINK href="http://i.imgur.com/nDf7a.jpg"]]http://i.imgur.com/nDf7a.jpg[[/LINK]]

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 roles <strong>media</strong> or <strong>press</strong>

This is because I want <strong>administrators</strong> and <strong>editors</strong> to still have access, but not <strong>media</strong> or <strong>press</strong>.

When I wan't to exit maintenance mode, I will simply remove my function.


Can any one help please?

Thanks Josh


<hr />


<strong>I have managed to figure it out, half way!</strong>

I just need help with the custom message after redirect back to the login page.

See below function which blocks out all my users from loggin in that are the user role <strong>press</strong> and <strong>media</strong>.


add_action( 'get_header', 'site_maintenance' );
function site_maintenance() {

if ( current_user_can('media') || current_user_can('press') ) {

// vars
$logout_url = wp_login_url();

wp_redirect( $logout_url, 302 );

$message = "Site undergoing maintainance.";
return $message;

}

}



Can anyone help me make the message work?


Many Thanks

Answers (3)

2013-01-15

Naveen Chand answers:

There is an excellent plugin here: [[LINK href="http://wordpress.org/extend/plugins/underconstruction/"]]http://wordpress.org/extend/plugins/underconstruction/[[/LINK]]

You can restrict which users will be able to access. Others will get a message under construction. You can have your own html message for the users whom you don't want to login.

I myself use this plugin a lot of times. You can try this.


Josh Cranwell comments:

Thank you.

It is an option but unfortunately only gives me the option to lock out either 1 user role or all user roles.

I need to block out specifically 2 user roles and allow the rest.

And if the message could appear here [[LINK href="http://i.imgur.com/nDf7a.jpg"]]http://i.imgur.com/nDf7a.jpg[[/LINK]] would be awesome.

Thanks for your suggestion.


Naveen Chand comments:

Here is a screenshot of how you can set the restriction in the Plugin's settings page.


Naveen Chand comments:

Sorry the screenshot didnt get attached properly. Here is a link to how to set that option: [[LINK href="https://docs.google.com/file/d/0ByCWdL1GiTHlOXVnSVNhWkE2TkU/edit"]]Screenshot[[/LINK]]

For example if you dropdown here and set it as Editor, then Editor and Above will be able to login.


Naveen Chand comments:

In addition to the plugin I have suggested you can add this code to your functions.php to get <strong>Site undergoing maintainance</strong> message:


function custom_login_message() {
$message = "Site undergoing maintainance";
return $message;
}
add_filter('login_message', 'custom_login_message');


Josh Cranwell comments:

@Naveen

It doesn't work.

If I select Author, then my custom roles: Press & Media can still loggin.

I only want to block custom <strong>Press</strong> & <strong>Media</strong> roles


Naveen Chand comments:

What capabilities does your custom roles have? Check the capabilities here: [[LINK href="http://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table"]]http://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table[[/LINK]]

If your Press and Media roles have capabilities higher than Author level, the they will surely be able to login. So you need to check what capability they have. If they have higher capabilities, then you may have to change their capabilities for the time the site is under maintenance.


Josh Cranwell comments:

See media role capabilities: http://i.imgur.com/CEV2D.png

And these are my settings: http://i.imgur.com/iKbPX.png

Surely what I'm asking can be done by a single function surely?

Using php like this combined with your message function?

current_user_can('media') || current_user_can('press')


Josh Cranwell comments:

I noticed it is off in my screenshot [[LINK href="http://i.imgur.com/iKbPX.png"]]http://i.imgur.com/iKbPX.png[[/LINK]]

But when I turn it on I get a holding page, and if I got to http://mysite.com/wp/wp-admin then my user can still log in to the admin.

I'm after a solid function ideally if its possible. Not sure this can do what I need it it to.

Thanks

2013-01-16

Dbranes answers:

hi, maybe something like

function my_login_message() {
if ( current_user_can('media') || current_user_can('press') ) {
$message = '<p class="message">Site undergoing maintainance.</p>';
return $message;
}
}
add_filter('login_message', 'my_login_message');


Josh Cranwell comments:

Awesome thanks dude.

2013-01-16

Arnav Joy answers:

try this

<?php


add_action( 'get_header', 'site_maintenance' );

function site_maintenance() {



if ( current_user_can('media') || current_user_can('press') ) {



// vars
if( $_GET['url'] == 'logout' ){
$message = "Site undergoing maintainance.";

echo $message;
}else {
$logout_url = wp_login_url().'?url=logout';

// try following if above code not worked

// $logout_url = wp_login_url().'&url=logout';



wp_redirect( $logout_url, 302 );


}




}



}
?>