Reputation: 31
I have Multisite Wordpress website and I've seen the default registration form but it did not meet all of my requirements because I want to add more additional fields like Password field.
Upvotes: 0
Views: 2156
Reputation: 376
To add custom fields in the registration form of a WordPress Multisite you need to do several things:
The file that controls the sign up process is wp-signup.php, where you can find useful filters and hooks to attach almost all the actions you need:
signup_extra_fields
.add_signup_meta
.wpmu_activate_user
.For the fields validation step I haven't found a filter or an action hook to run the necessary code. So I have added one to wp-signup.php that I call signup_validate_extra_fields
. You can call it as you want. I've added it inside the function validate_user_signup()
. This function calls the function validate_user_form()
that validates only username and user mail. The idea is to add the other fields validation at this point filtering this function. We can do this by replacing just one line (line 648):
$result = validate_user_form();
With this modification:
$result = apply_filters('signup_validate_extra_fields',validate_user_form());
With the new line, the beginning of the validate_user_signup()
funcion would be:
/**
* Validate the new user signup
*
* @since MU (3.0.0)
*
* @return bool True if new user signup was validated, false if error
*/
function validate_user_signup() {
// $result = validate_user_form();
$result = apply_filters('signup_validate_extra_fields',validate_user_form());
$user_name = $result['user_name'];
$user_email = $result['user_email'];
$errors = $result['errors'];
It is not a good practice to modify WordPress core files, of course. To avoid it and not to loose the modifications after every WordPress update, I use the plugin Network Subsite User Registration that uses a local copy of wp-signup.php, so I can add the new filter there and not in the core file.
The whole code using the hooks and filters I mention could be included in the file functions.php
of a theme or in a plugin (probably better):
// add validation for extra fields
add_action( 'signup_extra_fields', 'prefix_signup_extra_fields',10,1 );
function prefix_signup_extra_fields($errors) {
$first_name = ( ! empty( $_POST['first_name'] ) ) ? sanitize_text_field( $_POST['first_name'] ) : '';
$last_name = ( ! empty( $_POST['last_name'] ) ) ? sanitize_text_field( $_POST['last_name'] ) : '';
// extra fields error handle
$fields = array(
'first_name',
'last_name'
);
$e = '';
if ( isset( $errors ) ) {
foreach ( $fields as $f )
$e[$f] = ( $errors->get_error_message( $f ) ) ? '<p class="error">'.$errors->get_error_message( $f ).'</p>' : '';
}
else {
foreach ( $fields as $f )
$e[$f] = '';
}
$signup_out = '
<hr>
<p>
<label for="first_name">'.__( 'First name' ).
$e['first_name']
.'<input type="text" name="first_name" id="first_name" class="signup-input" value="'.esc_attr( $first_name ).'" required />
</p>
<p>
<label for="last_name">'.__( 'Last name' ).
$e['last_name']
.'<input type="text" name="last_name" id="last_name" class="signup-input" value="'.esc_attr( $last_name ).'" required />
</p>
';
echo $signup_out;
return;
}
// add validation for extra fields
// I added a new filter to wp-singup.php
add_filter('signup_validate_extra_fields', 'prefix_signup_validation', 10, 1);
function pdigital_signup_validation( $arguments ) {
if ( ! isset( $_POST['signup_for'] ) )
return;
$fields = array(
'first_name' => __('You must include a first name.','pdigital'),
'last_name' => __('You must include a last name.','pdigital')
);
$errors = $arguments['errors'];
foreach( $fields as $f => $e )
if ( empty( $_POST[$f] ) || ! empty( $_POST[$f] ) && trim( $_POST[$f] ) == '' )
$errors->add( $f, $e );
return $arguments;
}
// store extra fields in wp_signups table while activating user
add_filter('add_signup_meta', 'prefix_add_signup_meta');
function prefix_add_signup_meta($meta) {
$fields = array(
'first_name',
'last_name',
);
foreach ( $fields as $f )
if( isset( $_POST[$f] ) )
$meta[$f] = sanitize_text_field( $_POST[$f] );
return $meta;
}
// add extra fields to user profile when user is activated
add_action('wpmu_activate_user','prefix_activate_user',10,3);
function prefix_activate_user( $user_id, $password, $meta ) {
$fields = array(
'first_name',
'last_name'
);
foreach ( $fields as $f )
if ( isset( $meta[$f] ) )
update_user_meta( $user_id, $f, $meta[$f] );
return;
}
Customize the sign up form of a WordPress Multisite is trickier than doing it in a one-site WordPress.
Finally it is worth to read the documentation about user system in WordPress Multisite and understand that it is common to all the sites of the network, before adding additional fields to the registration form.
Upvotes: 3