• Resolved Sajid Manzoor

    (@sajiddesigner)


    Hello

    I am trying to add a custom field to registration form.
    I have done following changes in page-signup.php file

    // CUSTOM FIELD VIEW
    // Added a custom field $cf_org
    
    function show_user_form( $user_name = '', $user_email = '', $cf_org = '', $errors = '' ) {
    	if ( ! is_wp_error( $errors ) ) {
    		$errors = new WP_Error();
    	}
    
    	// User name
    	echo '<label for="user_name">' . __( 'Username:' ) . '</label>';
    	$errmsg = $errors->get_error_message( 'user_name' );
    	if ( $errmsg ) {
    		echo '<p class="error">' . $errmsg . '</p>';
    	}
    	echo '<input name="user_name" type="text" id="user_name" value="' . esc_attr( $user_name ) . '" autocapitalize="none" autocorrect="off" maxlength="60" /><br />';
    	_e( '(Must be at least 4 characters using lowercase letters and numbers only.)' );
    	?>
    
    	<label for="user_email"><?php _e( 'Email&nbsp;Address:' ); ?></label>
    	<?php
    	$errmsg = $errors->get_error_message( 'user_email' );
    	if ( $errmsg ) {
    		?>
    		<p class="error"><?php echo $errmsg; ?></p>
    	<?php } ?>
    	<input name="user_email" type="email" id="user_email" value="<?php echo esc_attr( $user_email ); ?>" maxlength="200" /><br /><?php _e( 'We send your registration email to this address. (Double-check your email address before continuing.)' ); ?>
    	<?php
    	$errmsg = $errors->get_error_message( 'generic' );
    	if ( $errmsg ) {
    		echo '<p class="error">' . $errmsg . '</p>';
    	}
    
    	// CU FIELD
    	$team_posts = get_posts([
    		'post_type' => 'wc_memberships_team',
    		'post_status' => 'publish',
    		'numberposts' => -1,
    		'orderby'=> 'title',
    		'order' => 'ASC'
    	]);
    	$teams = [];
    	$team_posts = check_duplicate_teams($team_posts); 
    	?>
    
    	<label for="cf_org "><?php _e( 'Organization:' ); ?></label>
    	<?php
    	$errmsg = $errors->get_error_message( 'cf_org ' );
    	if ( $errmsg ) {
    		?>
    		<p class="error"><?php echo $errmsg; ?></p>
    	<?php } ?>
    	<select name="cf_org " id="cf_org ">
    		<option value=""> -- Select your Organization -- </option>
    		<?php 
       // An array coming from PHP with values 
       foreach( $team_posts as $team){ ?>
    			<option value="<?php echo $team ?>"> <?php echo $team ?> </option>
    		<?php } ?>
    	</select>
    	<?php
    	$errmsg = $errors->get_error_message( 'generic' );
    	if ( $errmsg ) {
    		echo '<p class="error">' . $errmsg . '</p>';
    	}
    
    	
    	/**
    	 * Fires at the end of the user registration form on the site sign-up form.
    	 *
    	 * @since 3.0.0
    	 *
    	 * @param WP_Error $errors A WP_Error object containing 'user_name' or 'user_email' errors.
    	 */
    	do_action( 'signup_extra_fields', $errors );
    }

    After adding above code, i am able to see a custom field in User Registraion form. But data is not saving into database.

    I have further elaborated the code and found that data is being saved using wpmu_signup_user() So i have added custom field value but still it is not saving. see below code.

     function validate_user_signup() {
    	$result     = validate_user_form();
    	$user_name  = $result['user_name'];
    	$user_email = $result['user_email'];
    	$cf_org = $_POST['cf_org '];
    	$errors     = $result['errors'];
    
    	if ( $errors->has_errors() ) {
    		signup_user( $user_name, $user_email, $cf_org , $errors );
    		return false;
    	}
    
    	if ( 'blog' == $_POST['signup_for'] ) {
    		signup_blog( $user_name, $user_email );
    		return false;
    	}
    
    //echo 'cu';
    //print_r($cf_org );
    	//die('');
    	/** This filter is documented in wp-signup.php */
    	wpmu_signup_user( $user_name, $user_email, apply_filters( 'add_signup_meta', array('cf_org ' => $cf_org ) ) );
    
    	confirm_user_signup( $user_name, $user_email );
    	return true;
    }
    

    Please confirm, what steps do i need to do to save this value in Database or where i am doing wrong.

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Justin Fletcher

    (@justinticktock)

    Sorry I don’t have much time to help at the moment, however, I think you would want to look at the function ‘update_user_meta’.

    Plugin Author Justin Fletcher

    (@justinticktock)

    I’m going to mark this as resolved as the customisation of the registration form is not part of the NSUR plugin. Come back if you have other comments or let us know how you got on.

    Thread Starter Sajid Manzoor

    (@sajiddesigner)

    Hello @justinticktock

    Its very sad that you are not willing to help others. As a plugin author you should be ready to provide help to others.

    Sadly, there are not hooks/filters are created for this plugin, so that others can easily modify NSUR plugin form. If you can’t help, then you should add a proper documentation with FAQs answers with this plugin.

    I have seen other similar queries for this plugin and i have found your same behavior on other support questions as well.

    Thread Starter Sajid Manzoor

    (@sajiddesigner)

    For all the other users, who are have similar issues like me. I have solved this issue by using wpmu_activate_user action. I am on a multisite network so i am using wpmu_activate_user. Other user please look similar alternative to wpmu_activate_user.

    Code Example:

    add_action( 'wpmu_activate_user', 'occr_update_user_meta', 10, 3 );
    function occr_update_user_meta( $user_id, $password, $meta ){
    	// action...	
    	add_user_meta( $user_id, 'cf_org', $meta['cf_org'], false );
    }
    Plugin Author Justin Fletcher

    (@justinticktock)

    Hi?@sajiddesigner,? Please don’t feel very sad, I am willing where possible to help. It’s just that you were quiet for a week so all being well you found a solution to your problem.? As I said come back and let us know how you got on and?it looks like?update_user_meta did it for you, which is good.

    don’t forget that when you use ‘wpmu_activate_user’ that will only happen for the user creation on the network not each time a user joins a different sub-site.

    when you say "Sadly, there are not hooks/filters are created for this plugin" what are you hoping for?

    The Plugin has available?the standard WordPress 13-action & 10-filter hooks for the registration form and a few of its own that you’ll see in the code like the default role given on registration etc. But my main aim is to keep things to the standard WordPress way as much as possible so that people and read the WordPress documentation to get to where they want to go.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add custom field to Registration form’ is closed to new replies.