Viewing 12 replies - 16 through 27 (of 27 total)
  • Let me remark that the hooks for multisite registration are different than the ones for non-multisite registration.

    Let me list the hooks that I found in wp-signup.php
    action hooks: signup_header, before_signup_form, signup_blogform, signup_extra_fields, signup_hidden_fields, signup_finished, preprocess_signup_form, after_signup_form

    filter hooks: signup_another_blog_init, signup_create_blog_meta, add_signup_meta, signup_user_init, add_signup_meta, signup_blog_init, add_signup_meta, wpmu_active_signup

    Most of these hooks are not documented like the non-multisite hooks. But I believe I can use the hooks above to customize the registration form.

    Hoom@n –
    I see an error in your implementation of wpmu_validate_user_signup hook. The filter will only pass one parameter to your function. It is an array:

    $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);

    In your function you will want to examine the ‘errors’. You can add or remove errors.

    Thread Starter hoomn

    (@hoomn)

    Dear Ipstenu,

    Thanks a lot for signup_extra_fields, I didn’t know about it! Now, I can add my custom fields:

    add_action('signup_extra_fields','myplugin_register_form');
        function myplugin_register_form (){
            $first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
    		$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
    		$validation_code = ( isset( $_POST['validation_code'] ) ) ? $_POST['validation_code']: '';
    		?>
            <p>
                <label for="first_name"><?php _e('First Name','mydomain') ?><br />
                <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr(stripslashes($first_name)); ?>" size="25" /></label>
    		</p>
    		<p>
    			<label for="last_name"><?php _e('Last Name','mydomain') ?><br />
                <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(stripslashes($last_name)); ?>" size="25" /></label>
            </p>
    		<p>
    			<label for="validation_code"><?php _e('Validation Code','mydomain') ?><br />
                <input type="text" name="validation_code" id="validation_code" class="input" value="<?php echo esc_attr(stripslashes($validation_code)); ?>" size="25" /></label>
            </p>
            <?php
        }

    (The code works well, but could you please confirm if it’s OK or not? Specially, if all the variables are necessary and defined in a correct way.)

    Thread Starter hoomn

    (@hoomn)

    Dear jkhongusc,

    Thank you very much for your explanation.

    That’t right, and it seems that wpmu_validate_user_signup is not the correct hook for my current purpose. (By the way, can I use that wpmu_validate_user_signup to allow all numeric usernames and/or change the minimum username length? Or, do you know another hook to allow all numeric usernames and/or change the minimum username length?)

    So, how should I make all my extra form items required? And, how should I add an extra validation for the username? (i.e. allowing only a numeric username between x and y)

    P.S. As a reminder, here is my code for this purpose which works fine on a sigle site installation:

    add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
        function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
            if ( $_POST['user_login'] < 8000000000 || $_POST['user_login'] > 10000000000 )
                $errors->add( 'user_login_error', __('<strong>ERROR</strong>: not valid username','mydomain') );
            if ( empty( $_POST['first_name'] ) )
                $errors->add( 'first_name_error', __('<strong>ERROR</strong>: You must include a first name.','mydomain') );
            if ( empty( $_POST['last_name'] ) )
                $errors->add( 'last_name_error', __('<strong>ERROR</strong>: You must include a last name.','mydomain') );
            if ( $_POST['validation_code'] != $_POST['user_login'] )
                $errors->add( 'validation_code_error', __('<strong>ERROR</strong>: not correct','mydomain') );
    		return $errors;
        }

    Thread Starter hoomn

    (@hoomn)

    Finally, I could fixed the validation step:

    function myplugin_registration_errors ($result) {
    	$error_name = $result[ 'errors' ]->get_error_message( 'user_name' );
    	if ( $error_name == __('Sorry, usernames must have letters too!') ) {
    		unset ( $result[ 'errors' ]->errors[ 'user_name' ] );
    		if ( $_POST['user_name'] < 8000000000 || $_POST['user_name'] > 10000000000 )
    			$result['errors']->add( 'user_name', __('<strong>ERROR</strong>: Username is not valid.','mydomain') );
    	}
    	if ( empty( $_POST['first_name'] ) )
    		$result['errors']->add( 'first_name', __('<strong>ERROR</strong>: You must include a first name.','mydomain') );
    	if ( empty( $_POST['last_name'] ) )
    		$result['errors']->add( 'last_name', __('<strong>ERROR</strong>: You must include a last name.','mydomain') );
    	if ( $_POST['validation_code'] != $_POST['user_name'] )
    		$result['errors']->add( 'validation_code', __('<strong>ERROR</strong>: not valid.','mydomain') );
    	return $result;
    }

    But, WordPress doesn’t check to see if the username has been already used anymore!
    What’s wrong? And, how can I fix it? Should I copy the following code from ms-functions.php to my function? Or, is there a better way to fix this issue?

    // Check if the username has been used already.
    if ( username_exists($user_name) )
    	$errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
    
    // Has someone already signed up for this username?
    $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
    if ( $signup != null ) {
    	$registered_at =  mysql2date('U', $signup->registered);
    	$now = current_time( 'timestamp', true );
    	$diff = $now - $registered_at;
    	// If registered more than two days ago, cancel registration and let this signup go through.
    	if ( $diff > 2 * DAY_IN_SECONDS )
    		$wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
    	else
    		$errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
    
    	if ( $signup->active == 0 && $signup->user_email == $user_email )
    		$errors->add('user_email_used', __('username and email used'));
    }

    —————
    Furthermore, since user_register is not called in a multisite installation, the third part of my plugin is not working anymore and I can’t save the extra registration user meta.
    Could you please tell me where I should hook and how I can update the user meta?

    Hoom@n –
    > But, WordPress doesn’t check to see if the username has been already used anymore!

    I had no problems inserting my validation. I tried registering an existing username and/or email address and I got an error during the signup stage. I added some sample code below which also does validation on extra fields. I copied/pasted and edited it, this so there might be errors:

    add_filter('wpmu_validate_user_signup',my_wpmu_validate_user_signup',10,3);
    add_filter('signup_extra_fields','my_signup_extra_fields',10,1);
    add_filter('add_signup_meta','my_add_signup_meta');
    
    function my_signup_extra_fields($errors) {
        echo ' <label for="user_extra1">Extra:</label> ';
        if ( $errmsg = $errors->get_error_message('user_extra1') ) {
            echo ' <p class="error">'.$errmsg.'</p>';
        }
         echo ' <input name="user_extra1" type="text" id="user_extra1" value="" maxlength="200" /><br /><p> ';
    }
    function my_wpmu_validate_user_signup($result) {
        $extra1 = $_POST["user_extra1"];
        if ( empty( $extra1) )
            $errors->add('user_extra1', __( 'Please enter a extra1.' ) );
        }
        return $result;
    }
    function my_add_signup_meta($meta) {
        $data = $_POST['user_extra1'];
        $my_meta = array('extra1' => $data);
        $meta = array_merge($my_meta, $meta);
        return $meta;
    }
    Thread Starter hoomn

    (@hoomn)

    Dear jkhongusc,

    I have that problem (WordPress not checking for duplicate usernames) with numeric usernames. I think it might be because WordPress first checks to see if it’s a numeric username, and then it goes to see if it’s already exist or not. (The problem is definitely with the first if in my myplugin_registration_errors you can see above.)

    And, about the extra user meta, I tried your method for first_name and last_name, but the first/last name fields in users profile were still empty after signup. Is there any way to get the $user_id and use update_user_meta()? (e.g. by hooking to wpmu_new_user?)
    That would be more beneficial for me since I also want to use add_user_to_blog(). (I want to automatically add users to blogs based on their username.)

    Thread Starter hoomn

    (@hoomn)

    Could you please help me with this last part of my plugin? Don’t you have any idea dear Ipstenu?

    > Could you please help me with this last part of my plugin?

    What is the last part that you need help with?

    FYI, I have very limited knowledge of customizing the signup. I just started looking into this when I made my first post here. I was able to add extra fields to the signup and save the data as user meta data.

    Thread Starter hoomn

    (@hoomn)

    Thank you for your interest.

    As I mentioned in the 23rd post:

    • I can’t save the extra fields (first_name and last_name) into the users profiles.
    • I need the $user_id in order to use add_user_to_blog() and automatically add users to some blogs after their registration, but I don’t exactly know where too hook and how to retrieve the $user_id. (BTW, I think it would be better if I could add users to blogs after the email confirmation.)
    • And, the first if in the 18th post is preventing WordPress from checking for duplicate usernames. Is there any better fix for this issue than my suggestion in that post?

    To save the extra fields…
    continuing from my previous example where I saved the extra fields as meta data:

    add_action('wpmu_activate_user','my_wpmu_activate_user', 10, 3);
    function uscs_wpmu_activate_user($userid, $password, $meta) {
        if ( !empty($meta['extra1'])) {
            update_user_meta($userid, "extra1",$meta['extra1']);
        }
    }

    Instead of ‘extra1’ you can use first_name or last_name or any other user meta data. In addition, that is probably a good hook to add your add_user_to_blog().

    There are many different ways to get the user_id, either directly or indirectly. get_current_user_id(), get_current_user(), get_user_by(), etc. You can lookup these functions in the codex.

    I cannot help you on the last problem (numeric usernames). Don’t know what is happening.

    Thread Starter hoomn

    (@hoomn)

    Thank you very much for your help!

    Now, I can add users to blogs and save the extra fields into their profiles.

    The only remaining problem is that WordPress is not checking for duplicate usernames anymore…

Viewing 12 replies - 16 through 27 (of 27 total)

The topic ‘Custom Registration’ is closed to new replies.