WordPress.org

Ready to get started?Download WordPress

Forums

Custom Registration (28 posts)

  1. Hoom@n
    Member
    Posted 8 months ago #

    Hi,

    I want to customize my registration form (e.g. adding a required first name field as described here), but it seems that register_form and registration_errors are not compatible with WP MS.

    I tried using signup_header instead of register_form as suggested here, but I ended up by the form elements added to the top of registration page instead of bottom of the registration form.

    And, I tried using wpmu_validate_user_signup instead of registration_errors, but I ended up with a blank page instead of the error message after registration attempts.

    Is there any way to use the normal registration page for WP MS? (My plugin works great with the normal WP, and I don't want to allow the WP MS users to create blogs by themselves at registration.) Or, could you please tell me how I can customize the WP MS signup page?

    Thanks,
    Hooman

  2. Read the answer here: http://stackoverflow.com/questions/10339053/wordpress-multisite-how-to-add-custom-blog-options-to-add-new-site-form-in-ne

    it goes in to a lot of the options.

    I don't want to allow the WP MS users to create blogs by themselves at registration.)

    Well that's by default.

    /wp-admin/network/settings.php

    Under "Allow new registrations" pick "User accounts may be registered." or "Logged in users may register new sites."

  3. Hoom@n
    Member
    Posted 8 months ago #

    Thanks a lot for your quick response.

    That page seemed really good, but I think it was more complicated and less flexible in comparison with WP single site signup page options.

    And, about the settings, I'm using the exact same option; so I thought this less flexible WP MS signup page is useless for me, and I would prefer to have the normal registration page on my site.

    Is there any way to totally replace this MS signup page with the normal signup page? (It can add the users to my network, and then I'll assign them to blogs through my admin panel.)

  4. That would be the normal signup page. /wp-admin/network/settings.php

    Under "Allow new registrations" pick "User accounts may be registered."

  5. Hoom@n
    Member
    Posted 8 months ago #

    Thanks for your answer, but unfortunately, selecting "User accounts may be registered." doesn't make my WP MS signup page like the normal WP signup page for me.

    The signup page is located at /wp-login.php?action=register in the normal WP installation and works great, but that page is located at /wp-signup.php in my WP MS installation and doesn't work with my plugin.

    How can I tell WP MS to use the /wp-login.php?action=register instead of /wp-signup.php?

  6. You don't.

    You use wp-signup.php, and when you select "User accounts may be registered." , that pages CHANGES to hide the blog sign up.

  7. Hoom@n
    Member
    Posted 8 months ago #

    Dear Ipstenu,

    It DOES "hide the blog sign up", but it DOESN'T make register_form and and registration_errors work! And, I want the normal WP signup page only to use those hooks!

    Do you think it is possible to change the WP MS signup process in a way that makes those hooks working again?

  8. http://wordpress.stackexchange.com/questions/73258/how-to-customize-the-wp-signup-php-for-a-multisite-network

    (I gave you the wrong options link in the beginning, my bad)

    So register_form will (and should) still work. If it's not, make sure the problem isnt your code.

    I tried using signup_header instead of register_form as suggested here, but I ended up by the form elements added to the top of registration page instead of bottom of the registration form.

    To go back to this... You may be hooking in thr wrong place. Can you share YOUR code? :)

  9. tharsheblows
    Member
    Posted 6 months ago #

    Hi - I know what you mean, Hoom@n. I don't quite have an answer but inching towards one.

    Ipstenu, if I edit wp-signup.php, will WP updates overwrite it? Currently I'm customising with the idea that they will but if they don't, then you can edit wp-signup.php without fear (I have the fear so am using a hodge-podge of jQuery and css).

  10. if I edit wp-signup.php, will WP updates overwrite it?

    Yes.

    then you can edit wp-signup.php without fear

    You really should be terrified. Honest. Don't modify core WordPress files as it never ends well.

    Can you please start your own topic and describe what you are attempting to accomplish?

    http://wordpress.org/support/forum/multisite#postform

  11. tharsheblows
    Member
    Posted 6 months ago #

    Oh no! I am suitably terrified, believe me. I've been doing this on the assumption that updates can overwrite it, so have not touched it. I just wanted to triple check.

    I'm trying to accomplish exactly what hoom@n is - customise my login page but I am having styling issues as well.

  12. Read this: http://halfelf.org/2012/multisite-registration/

    When you get to "Adding Users to Sites" you can stop, that part doesn't matter. The first half about making a template, THAT is what you want to do.

  13. tharsheblows
    Member
    Posted 6 months ago #

    Thank you so much, that is exactly what I was looking for - the whole post includes answers to questions I had but hadn't asked here. I really really appreciate it, was banging my head against the wall - and there is so much bad information about wp-signup.php out there I had given up looking.

  14. Hoom@n
    Member
    Posted 5 months ago #

    This is my code for a single site installation, which works well:

    <?php
    
    	/*
    	Plugin Name: Custom Registration
    	Description: This plugin customizes WordPress's built-in user registration page.
    	Author: Hooman Tadbiri
    	Version: 0.1
    	*/
    
        //1. Add a new form element...
        add_action('register_form','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
        }
    
        //2. Add validation. In this case, we make sure first_name is required.
        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;
        }
    
        //3. Finally, save our extra registration user meta.
        add_action('user_register', 'myplugin_user_register');
        function myplugin_user_register ($user_id) {
            if ( isset( $_POST['first_name'] ) )
                update_user_meta($user_id, 'first_name', $_POST['first_name']);
    		if ( isset( $_POST['last_name'] ) )
                update_user_meta($user_id, 'last_name', $_POST['last_name']);
        }
    
    ?>

    (Usernames must be numeric, and users must enter a validation code which is their username for instance.)

    And, this is my code for a multi-site installation, which doesn't work:

    <?php
    
    	/*
    	Plugin Name: Custom Registration
    	Description: This plugin customizes WordPress's built-in user registration page.
    	Author: Hooman Tadbiri
    	Version: 0.1
    	*/
    
        //1. Add a new form element...
        add_action('signup_header','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
        }
    
        //2. Add validation. In this case, we make sure first_name is required.
        add_filter('wpmu_validate_user_signup', '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 2','mydomain') );
    		return $errors;
        }
    
        //3. Finally, save our extra registration user meta.
        add_action('user_register', 'myplugin_user_register');
        function myplugin_user_register ($user_id) {
            if ( isset( $_POST['first_name'] ) )
                update_user_meta($user_id, 'first_name', $_POST['first_name']);
    		if ( isset( $_POST['last_name'] ) )
                update_user_meta($user_id, 'last_name', $_POST['last_name']);
        }
    
    ?>

    As a reminder, I don't want to let my users create new blogs and I just need the "User accounts may be registered.". The only thing I want more is to add first name, last name, and a validation field to the registration page.

  15. Why not use signup_extra_fields ?

  16. jkhongusc
    Member
    Posted 5 months ago #

    I am looking into customizing the registration form for WP multisite. I can confirm that register_form and register_post are NOT called in WP multisite.

    However signup_extra_fields is called

  17. jkhongusc
    Member
    Posted 5 months ago #

    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.

  18. jkhongusc
    Member
    Posted 5 months ago #

    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.

  19. Hoom@n
    Member
    Posted 5 months ago #

    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.)

  20. Hoom@n
    Member
    Posted 5 months ago #

    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;
        }
  21. Hoom@n
    Member
    Posted 5 months ago #

    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?

  22. jkhongusc
    Member
    Posted 5 months ago #

    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;
    }
  23. Hoom@n
    Member
    Posted 5 months ago #

    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.)

  24. Hoom@n
    Member
    Posted 5 months ago #

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

  25. jkhongusc
    Member
    Posted 5 months ago #

    > 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.

  26. Hoom@n
    Member
    Posted 5 months ago #

    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?
  27. jkhongusc
    Member
    Posted 5 months ago #

    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.

  28. Hoom@n
    Member
    Posted 5 months ago #

    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...

Reply

You must log in to post.

About this Topic