0

I need to know if there's a way to remove the register button on the WordPress login page along with the link to the registration form ... without messing with core files ... so that I don't get just "anyone" (or spammers) setting up accounts!

I do, however, still need the user registration function to work.

I have a "private" photo uploader that is locked to anyone who doesn't have an account with the site ... but, the only way (the only way I'm wanting that is) new users to register is to be given the login information for a custom user that unlocks the registration form. Therefore limiting users to "invites" only.

NOTE: I'm working in a plugin already, so I cannot just use another plugin as it is bad practice. This must be something that I can put in a functions.php file or something like that.


UPDATE:

Based on the answer given by @Stephen Harris I've looked into using the wp_create_user function ... However, I'm getting a little confused as to how I should go about adding it to my existing "customized" register form (this will be completely separate from the default register form).

Here is my current form code:

<!-- Registration form -->
<h1 class="register-header">Register a new user</h1>
<?php $register = $_GET['register']; 
    if($register == true) { 
        echo '<p class="register-success">Check your email for the password! Logout and use your new username & password.</p>'; 
    }?>
<a class="logout" href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a>

<form method="post" id="register_form" class="wp-user-form" action="echo site_url('wp-login.php?action=register', 'login_post') ?>">
    <!-- Username -->
    <p>
        <input type="text" name="user_login" id="user_login" class="input" placeholder="unique username" />
    </p>
    <!-- Email to send p/w to -->
    <p>
        <input type="email" name="user_email" id="user_email" class="input" placeholder="email address" />
    </p>

    <p class="small-text">You will receive an email with a generated password<br />(which you can change in your "user settings")</p>

    <?php do_action('register_form'); ?>
    <button name="wp-submit" id="wp-submit" class="btn">Register new user</button>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
    <input type="hidden" name="user-cookie" value="1" />

</form><!-- end registration-form -->

This all works when "anyone can register" is turned on ... but how do I make it work when I turn it "off"?

3 Answers 3

1

There's a "Membership" setting under Settings > General. By disabling "Anyone can register" the public-facing registration page will be effectively removed, along with the 'register' button.

The API, however, will still be usable (e.g. wp_create_user().)

On a side note, you mention

NOTE: I'm working in a plugin already, so I cannot just use another plugin as it is bad practice

If you mean, you're creating a plug-in for distribution and don't want a third-party dependency, that's understandable. But in general there is nothing wrong in using multiple plug-ins to achieve a desired result.

1
  • I've been looking into the info you gave me ... wp_create_user ... but I'm getting a little confused as how to implement it!? I've updated my question so you can see the code I'm working with. Thanks! May 14, 2014 at 13:59
0

I suspect that the answer by @StephenHarris is probably correct that you can just switch off public registration and have your code us the API, but there is a way to remove the registration link from just page.

If you look at the source for the Core login page you will see this:

541 if ( get_option( 'users_can_register' ) ) :
542         $registration_url = sprintf( '<a href="%s">%s</a>', esc_url( wp_registration_url() ), __( 'Register' ) );

If you can get that call to get_option() to return false the link will vanish. get_option() provides a hook that will allow that.

45            $pre = apply_filters( 'pre_option_' . $option, false );
46            if ( false !== $pre )
47                    return $pre;

https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/option.php#L45

Well, sort of. Returning false from that hook won't quite work, but returning null will.

And the login_init action will allow you to restrict the effect to the one page. So...

add_action(
  'login_init',
  function() {
    add_filter('pre_option_users_can_register','__return_null');
  }
);

Reference: http://codex.wordpress.org/Function_Reference/_return_null

0

After doing quite a bit of research and LOTS of reading (and re-reading) I was, finally, able to figure out how to get wp_create_user to work!

Below you'll see the code that I'm using to make this happen:

<!-- Registration form -->
<h1 class="register-header">Register a new user</h1>
<?php $register = $_GET['register']; 
if($register == true) { 
    echo '<p class="register-success">Check your email for the password! Logout and use your new username & password.</p>'; 
}?>
<a class="logout" href="<?php echo wp_logout_url( get_permalink() ); ?>">Logout</a>

<form method="post" id="register_form" class="wp-user-form" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
    <!-- Username -->
    <p>
        <input type="text" name="user_login" id="user_login" class="input" placeholder="unique username" />
    </p>
    <!-- Email to send p/w to -->
    <p>
        <input type="email" name="user_email" id="user_email" class="input" placeholder="email address" />
    </p>

    <p class="small-text">You will receive an email with a generated password<br />(which you can change in your "user settings")</p>
    <button name="wp-submit" id="wp-submit" class="btn">Register new user</button>
</form>

<?php 
$username = $_POST['user_login'];
$email = $_POST['user_email'];

if (( $username != '' ) && ( $email != '' )){

    $user_id = username_exists( $username );
    $user_email = email_exists( $email );

    if (( !$user_id ) && ( !$user_email )) {

        // Generate the password and create the user
        $password = wp_generate_password( 12, false );
        $user_id = wp_create_user( $username, $password, $email );

        // Update user
        wp_update_user( array( 'ID' => $user_id ));

        // Set user role
        $user = new WP_User( $user_id );
        $user->set_role( 'subscriber' );

        // Email user their password
        wp_mail( $email, 'Your new password!', 'Here\'s your new password: ' . $password );
    } else {

        $password = __( 'User already exists.' );
    } 
}?>
<!-- end registration-form -->

This handles creating a new user great! But I have to work on error-handling.

If anyone has tips or corrections to how I'm doing this please let me know ... I'm still trying to wrap my thoughts around how this stuff works. ;)

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.