I have a certain condition in my code (if the user is visiting the page from a network range) and I can ask for this condition easily, let's say:

if ( $user_in_allowed_range)

What I want is to offer the registration button only to users who fulfill this condition, and other users should still be able to login.

Is there any way to do this in WordPress without hacking the core? An additional custom login form is not a solution as the original one is still available.

I found some hooks (login_form, login_head, user_register …) and the theme function wp_login_form() but they do not offer my needed feature.

1 Answer 1


Since get_option('users_can_register') will return the setting value for the "Anyone can register" setting used for displaying the registration link, you could probably add a get_option filter overriding the value for the users_can_register key. In this case i even think the pre_option_%s filter is your best bet, since the actual value of the settings is irrelevant. Something like this might work:

add_filter('pre_option_users_can_register', 'wpse_87341_users_can_register');

function wpse_87341_users_can_register() {
    // Obviously here you'd populate $user_in_allowed_range
    // ...

    return intval($user_in_allowed_range); // We need to return an int since get_option does not like false return values
  • Thank you for your answer, I will check it and come back with the result. :)
    – dexBerlin
    Commented Feb 18, 2013 at 10:03
  • Okay, it does not work yet. The function returns false, but I can still access /wp-login.php?action=register.
    – dexBerlin
    Commented Feb 18, 2013 at 10:21
  • btw.: I have added the code in my functions.php
    – dexBerlin
    Commented Feb 18, 2013 at 10:29
  • Whoops, turns out get_option validates the return value of pre_option_%s filters to make sure it is not explicitly false (i.e. checking !==), since this can also mean the value is inexistent. Returning either 0 or 1 instead appears to be working fine. I've updated my example to reflect this fact.
    – Simon
    Commented Feb 18, 2013 at 10:38

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.