Add custom form field to Pro Sites sign up form

Hi There,
I know this is a bit of an age old topic on here, but I'm determined to make this work with a custom plugin. I am trying to add a select box to the user registration form. The select box allows a user to select their occupation, and adds it to the user_meta table in the db.

I am using the signup password plugin you have made as a template, this allows me to easily output the html into the form without too much difficulty, what I'm struggling with is understanding how I would submit that data to the db (I know I would use add_user_meta() but, how do I get this to trigger when the user registration form is submitted).

Here is my code (edited from signup-password.php):

<?php
/*
Plugin Name: Signup I Am
Plugin URI: http://www.skizzar.com
Description: This plugin places custom user_meta box on the WP Site, WP Multisite or BuddyPress signup form forcing the user to select their business type
Author: Skizzar
Version: 1.0
Author URI: http://www.skizzar.com
Network: true
*/ 

global $signup_i_am_form_printed;

//------------------------------------------------------------------------//
//---Hook-----------------------------------------------------------------//
//------------------------------------------------------------------------//
add_action('template_redirect', 'skizzar_signup_i_am_init_sessions');
add_action('signup_extra_fields', 'skizzar_signup_i_am_fields');
add_filter('wpmu_validate_user_signup', 'skizzar_signup_i_am_filter');
add_filter('signup_blogform', 'skizzar_signup_i_am_fields_pass_through');
add_filter('add_signup_meta', 'skizzar_signup_i_am_meta_filter',99);
//------------------------------------------------------------------------//
//---Functions------------------------------------------------------------//
//------------------------------------------------------------------------//

//trigger error on activation
if ( !is_multisite() ) exit( 'Only compatible with wordpress multisite' );

function skizzar_signup_i_am_meta_filter($meta) {
	global $signup_i_am_use_encryption;
	$i_am_a = isset($_POST['i_am_a'])?$_POST['i_am_a']:'';
	if ( !empty( $i_am_a ) ) {
		$add_meta = array('password' => $i_am_a);
		$meta = array_merge($add_meta, $meta);
	}
	return $meta;
}

//------------------------------------------------------------------------//
//---Output Functions-----------------------------------------------------//
//------------------------------------------------------------------------//

if ( ! function_exists( 'signup_i_am_fields_pass_through' ) ) {
	function signup_i_am_fields_pass_through() {
		return skizzar_signup_i_am_fields_pass_through();
	}
}

function skizzar_signup_i_am_fields_pass_through() {
	global $signup_i_am_form_printed;

	if ( !empty( $_POST['i_am_a'] ) ) {
		$signup_i_am_form_printed = 1;
		?>
		<input type="hidden" name="i_am_a" value="<?php echo $_POST['i_am_a']; ?>" />
		<?php
		$_SESSION['i_am_a'] = $_POST['i_am_a'];
	} else if (isset($_SESSION['i_am_a']) && !empty($_SESSION['i_am_a'])) {
		$signup_i_am_form_printed = 1;
		?>
		<input type="hidden" name="i_am_a" value="<?php echo $_SESSION['i_am_a']; ?>" />
		<?php
	}
}

if ( ! function_exists( 'signup_i_am_fields' ) ) {
	function signup_i_am_fields() {
		return skizzar_signup_i_am_fields();
	}
}

function skizzar_signup_i_am_fields() {
	global $signup_i_am_form_printed;
	$signup_i_am_form_printed = 1;
	?>
    <label for="i_am_a">I am a:</label>
		<select name="i_am_a" id="i_am_a">
            <option value="musician">Musician/Artist</option>
            <option value="band">Band</option>
            <option value="photographer">Photographer</option>
            <option value="business">Small Business</option>
            <option value="other">Other</option>
        </select>
	<?php
}

if ( ! function_exists( 'signup_i_am_init_sessions' ) ) {
	function signup_i_am_init_sessions() {
		return skizzar_signup_i_am_init_sessions();
	}
}

function skizzar_signup_i_am_init_sessions() {
	if ( is_user_logged_in() ) return;

	if (!session_id()) {
		session_start();
	}
}
  • Adam Czajczyk
    • Support Gorilla

    Hello Samuel,

    I hope you're well today and thank you for your question!

    I checked your plugin on my own install and although it is showing an additional field, it's also causing a payment gateway error (payment form not showing up). This is an issue similar to this one here:


    https://premium.wpmudev.org/forums/topic/might-have-found-a-bug-in-the-pro-site-sign-up-without-wp-sign-up#post-1005699

    @Ben who is another member of our community is trying to achieve pretty much the same feature with Pro Sites with his own plugin. I've already asked the developer there to give us a helping hand. I hope the developer will be able to suggest a solution soon so you may want to keep an eye on both thread's - this one and the one I linked to.

    Best regards,
    Adam

    • Samuel
      • The Incredible Code Injector

      Hi @Adam Czajczyk,
      Thanks for your reply, and for copying me in on the post with Ben.

      I have actually found a solution that seems to work prefectly for me. Below is my code, this adds a first name, last name and drop down field to the registration form. It also saves all the info to the wp_usermeta table, and adds the custom dropdown to the edit profile page in the dashboard too.

      Hopefully people find it useful as there are a lot of threads about how to customize the registration form.

      <?php
      /*
      Plugin Name: Signup I Am
      Plugin URI: http://www.skizzar.com
      Description: This plugin places custom user_meta box on the WP Site, WP Multisite or BuddyPress signup form forcing the user to select their business type
      Author: Skizzar
      Version: 1.0
      Author URI: http://www.skizzar.com
      Network: true
      */ 
      
      // This function shows the form fiend on registration page
      //1. Add a new form element...
      add_action( 'signup_extra_fields', 'myplugin_register_form' );
      function myplugin_register_form() {
      
          $i_am_a = ( ! empty( $_POST['i_am_a'] ) ) ? trim( $_POST['i_am_a'] ) : '';
          $first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';
          $last_name = ( ! empty( $_POST['last_name'] ) ) ? trim( $_POST['last_name'] ) : '';
      ?>
      <style>
      .first_name, .last_name { width:49%; display:inline-block; }
      .first_name { float:left !important; margin-right:2%;}
      </style>
          <p>
              <label class="first_name" for="first_name">First Name:<br />
                  <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
          </p>
          <p>
              <label class="last_name" for="last_name">Last Name:<br />
                  <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label>
          </p>
          <p>
              <label for="i_am_a">I am a:</label>
                  <select name="i_am_a" id="i_am_a">
                      <option value="musician">Musician/Artist</option>
                      <option value="band">Band</option>
                      <option value="photographer">Photographer</option>
                      <option value="business">Small Business</option>
                      <option value="other">Other</option>
                  </select>
      
          </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 ( empty( $_POST['i_am_a'] ) || ! empty( $_POST['i_am_a'] ) && trim( $_POST['i_am_a'] ) == '' ) {
              $errors->add( 'i_am_a_error', __( '<strong>ERROR</strong>: You must pick a type for yourself', 'skizzar' ) );
          }
          if ( empty( $_POST['first_name'] ) || ! empty( $_POST['first_name'] ) && trim( $_POST['first_name'] ) == '' ) {
              $errors->add( 'first_name_error', __( '<strong>ERROR</strong>: You must include a first name.', 'skizzar' ) );
          }
          if ( empty( $_POST['last_name'] ) || ! empty( $_POST['last_name'] ) && trim( $_POST['last_name'] ) == '' ) {
              $errors->add( 'last_name_error', __( '<strong>ERROR</strong>: You must include a last name.', 'skizzar' ) );
          }
      
          return $errors;
      }
      
      //3. Finally, save our extra registration user meta.
      add_action( 'user_register', 'myplugin_user_register' );
      function myplugin_user_register( $user_id ) {
          if ( ! empty( $_POST['i_am_a'] ) ) {
              update_user_meta( $user_id, 'i_am_a', trim( $_POST['i_am_a'] ) );
          }
          if ( ! empty( $_POST['first_name'] ) ) {
              update_user_meta( $user_id, 'first_name', trim( $_POST['first_name'] ) );
          }
          if ( ! empty( $_POST['last_name'] ) ) {
              update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
          }
      }
      /*****************************/
      
      //hooks
      add_action( 'show_user_profile', 'Add_user_fields' );
      add_action( 'edit_user_profile', 'Add_user_fields' );
      
      function Add_user_fields( $user ) { ?>
      
      <h3>Extra fields</h3>
      <table class="form-table">
          <tr>
              <th><label for="dropdown">I Am A</label></th>
              <td>
                  <?php
                  //get dropdown saved value
                  $selected = get_user_meta( $user->ID, 'i_am_a', true );
                  ?>
      
                  <select name="i_am_a" id="i_am_a">
                      <option value="musician" autocomplete="off" <?php echo ($selected == "musician")?  'selected="selected"' : '' ?>>Musician/Artist</option>
                      <option value="band" autocomplete="off" <?php echo ($selected == "band")?  'selected="selected"' : '' ?>>Band</option>
                      <option value="photographer" autocomplete="off" <?php echo ($selected == "photographer")?  'selected="selected"' : '' ?>>Photographer</option>
                      <option value="business" autocomplete="off" <?php echo ($selected == "business")?  'selected="selected"' : '' ?>>Small Business</option>
                      <option value="other" autocomplete="off" <?php echo ($selected == "other")?  'selected="selected"' : '' ?>>Other</option>
                  </select>
              </td>
          </tr>
      </table>
      <?php }
      add_action( 'personal_options_update', 'save_user_fields' );
      add_action( 'edit_user_profile_update', 'save_user_fields' );
      
      function save_user_fields( $user_id ) {
      
      if ( !current_user_can( 'edit_user', $user_id ) )
          return false;
      
      //save dropdown
      update_usermeta( $user_id, 'i_am_a', $_POST['i_am_a'] );
      }
  • Ben
    • The Reaper

    Very nice @Samuel

    So..there are many ways up the mountain and this look like another great way that is compatible with Pro-Sites.

    I guess the question is what is the right standard way, using the method I wrote or this method. I realize this method "works" but for the future or when trying to integrate other add on's to the registration, should pro-sites be adhering to including the other standard methods. I'm just a general programmer new to wordpress within the last year, so I am not fully uptodate on everything, just what I am hacking together.

    Thanks!

    -Ben

  • Ben
    • The Reaper

    (oops thought i was posting on my own thread)
    I wrote some additional registration fields using "register_form" that were working for wordpress registration but weren't all the way adding in on the pro-sites sign up form for some reason.

    Nice work!

    • Samuel
      • The Incredible Code Injector

      @Ben Yes I agree, there does need to be a more standardised way to do this with ProSites - just a quick search will show you how much of a demand custom registrations forms are.

      At least this holds together for now - and thanks for the heads up on update_usermeta - i'll get that changed and add my code below.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.