WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

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
share|improve this answer
Thank you for your answer, I will check it and come back with the result. :) – dexBerlin Feb 18 '13 at 10:03
Okay, it does not work yet. The function returns false, but I can still access /wp-login.php?action=register. – dexBerlin Feb 18 '13 at 10:21
btw.: I have added the code in my functions.php – dexBerlin Feb 18 '13 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 Feb 18 '13 at 10:38
Thank you a lot for the edit, that solved the issue. – dexBerlin Feb 18 '13 at 10:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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