• I’ve got a custom field, which I’ve successfully implemented in the profile editor and it retrieves a list of options from a database and displays the user’s current setting and allows you to save a new setting. So that’s all fine. My problem is in trying to add that field to the user registration page. I’ve followed the instructions in the Codex and in several tutorials, using the three hooks they say I need, but I’m seeing no change on the user registration page at all.

    I’m adding the following code into the theme’s functions.php (just under the code I added to display the new field on the profile editor page).

    I haven’t finished adding customization code, so I know it isn’t 100% working yet, but I can’t even get it to display the field. Anyone have any idea what I might be doing wrong here?

    
    //Custom Registration Form Fields
    
    //1. Add a new form element...
    add_action( 'register_form', 'et_register_form' );
    function et_register_form()
    {
    	//$location = ( ! empty( $_POST['location'] ) ) ? trim( $_POST['location'] ) : '';
    		?>
    		<p>
    			<label for="location">Location<br />
    				<input type="text" name="location" id="location" class="input" value="baytown" size="25" />
    			</label>
    		</p>
    		<?php
    }
    
    //2. Add validation. In this case, we make sure location is required.
    add_filter( 'registration_errors', 'et_registration_errors', 10, 3 );
    function et_registration_errors( $errors, $sanitized_user_login, $user_email )
    {
    	
    	if ( empty( $_POST['location'] ) || ! empty( $_POST['location'] ) && trim( $_POST['location'] ) == '' )
    	{
    		$errors->add( 'location_error', 'You must include a location!' );
    	}
    	return $errors;
    }
    
    //3. Finally, save our extra registration user meta.
    add_action( 'user_register', 'et_user_register' );
    function et_user_register( $user_id )
    {
    	if ( ! empty( $_POST['location'] ) )
    	{
    		update_user_meta( $user_id, 'location', trim( $_POST['location'] ) );
    	}
    }
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The part that displays the field works fine on my site. There’s nothing wrong with it. I didn’t test further since this is the problem you are currently facing. Does your site have a custom login form? Your code may only work on the default form accessed through wp-login.php?action=register when this URL is not redirected to any custom form.

    If you are using the default form and not seeing a field, it may be other code is inappropriately using the same action in a way that prevents your action from working. If you revert your site to the default state (except for your own code posted here), you should see your code works fine. Restore your normal state, one module at a time. When the field disappears again, the last activated module is messing with the action somehow.

Viewing 1 replies (of 1 total)
  • The topic ‘Problem adding custom user registration fields’ is closed to new replies.