I have a site where, when a user is flagged as spam, that user account is blocked.

This is fine, but sometimes a user is blocked by mistake. When the user tries to log in, since the account is blocked, they just get the default Drupal "wrong username/password" message.

However, I would like to give a custom message ("Sorry, it looks like someone thought your account was spam. If this is a mistake, please contact us.") when the user account is inactive and attempts to log in. I'm using the latest version of Drupal 7 and I tried to find a way to do this in Rules but I couldn't come up with anything.

  • I think, you can create a new user role for the users that flagged as spam. Then set the permissions for this role as you want. And finally show any message for only this role while the user tries to log in. Does it seem good?
    – herci
    Jan 3, 2015 at 14:35
  • @herci Actually, in my implementation the user is already assigned a spam role, so that would work, but I still want to block the accounts (to prevent site-wide e-mails from being sent to such users accidentally, etc.) Jan 3, 2015 at 14:37
  • 1
    I get the message 'The username %name has not been activated or is blocked.' which is set in a validate function in user.module line 2128. Have a look at this: Remove or alter a message from drupal_set_message(). Check for the message and alter it. Jan 3, 2015 at 14:58
  • @J.Reynolds After some more debugging, I realised I wasn't getting the blocked message because of the email_registration module. But your tip is very useful, thanks! Jan 3, 2015 at 17:26

1 Answer 1


You can alter the login form and add a custom validator where you check if user is blocked and show the error message you want.

If you check the code you'll see that login validator are set by the user_login_default_validators() function. Those validators are:

  • user_login_name_validate
  • user_login_authenticate_validate
  • user_login_final_validate

What I don't understand is why are you getting the "wrong username/password" (actually the message is "Sorry, unrecognized username or password. Have you forgotten your password?"). The first validator checks if the user is blocked:

 * A FAPI validate handler. Sets an error if supplied username has been blocked.
function user_login_name_validate($form, &$form_state) {
  if (!empty($form_state['values']['name']) && user_is_blocked($form_state['values']['name'])) {
    // Blocked in user administration.
    form_set_error('name', t('The username %name has not been activated or is blocked.', array('%name' => $form_state['values']['name'])));

As you can see when a user is blocked the message es "The username %name has not been activated or is blocked".

Anyway, to achieve what you want I think you ca follow the steps:

Implement hook_form_FORM_ID_alter adding your custom validator as FIRST validator. You have to alter two forms, 'user_login' and 'user_form_block' (tose are the form ids). Use:

array_unshift($form['#validate'], 'my_custom_user_validation');

to add your custom validator as first validator.

Then code your custom validator. It should be same as user_login_name_validate but using your custom message.

Edit: As I've pointed out in my answer and J. Reynolds says the blocked message is not the standard wrong user/password. If you get the blocked user message you can go with J. Reynolds suggestion (just altering the standard user blocked message). If you need to check for any other different condition (for example, blocked and spam role) you can use what I say in this answer.

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.