Skip to content

Commit 0712579

Browse files
Users: Preserve entered values on Add Existing User form after validation errors
1 parent d2e7bcf commit 0712579

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/wp-admin/user-new.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,30 @@
3333
check_admin_referer( 'add-user', '_wpnonce_add-user' );
3434

3535
$user_details = null;
36-
$user_email = wp_unslash( $_REQUEST['email'] );
36+
$redirect_args = array();
37+
foreach ( array( 'email', 'role', 'noconfirmation' ) as $redirect_arg ) {
38+
if ( isset( $_REQUEST[ $redirect_arg ] ) ) {
39+
$redirect_args[ $redirect_arg ] = $_REQUEST[ $redirect_arg ];
40+
}
41+
}
42+
43+
$user_email = wp_unslash( $_REQUEST['email'] );
3744

3845
if ( str_contains( $user_email, '@' ) ) {
3946
$user_details = get_user_by( 'email', $user_email );
4047
} else {
4148
if ( current_user_can( 'manage_network_users' ) ) {
4249
$user_details = get_user_by( 'login', $user_email );
4350
} else {
44-
wp_redirect( add_query_arg( array( 'update' => 'enter_email' ), 'user-new.php' ) );
51+
$redirect_args['update'] = 'enter_email';
52+
wp_redirect( add_query_arg( $redirect_args, 'user-new.php' ) );
4553
die();
4654
}
4755
}
4856

4957
if ( ! $user_details ) {
50-
wp_redirect( add_query_arg( array( 'update' => 'does_not_exist' ), 'user-new.php' ) );
58+
$redirect_args['update'] = 'does_not_exist';
59+
wp_redirect( add_query_arg( $redirect_args, 'user-new.php' ) );
5160
die();
5261
}
5362

@@ -469,23 +478,34 @@
469478
<input name="action" type="hidden" value="adduser" />
470479
<?php wp_nonce_field( 'add-user', '_wpnonce_add-user' ); ?>
471480

481+
<?php
482+
$adduser_email = isset( $_GET['email'] ) ? wp_unslash( $_GET['email'] ) : '';
483+
$adduser_role = isset( $_GET['role'] ) ? sanitize_text_field( wp_unslash( $_GET['role'] ) ) : '';
484+
$adduser_noconfirmation = isset( $_GET['noconfirmation'] ) ? wp_unslash( $_GET['noconfirmation'] ) : '';
485+
if ( $adduser_role && ! array_key_exists( $adduser_role, get_editable_roles() ) ) {
486+
$adduser_role = '';
487+
}
488+
?>
472489
<table class="form-table" role="presentation">
473490
<tr class="form-field form-required">
474491
<th scope="row"><label for="adduser-email"><?php echo esc_html( $label ); ?></label></th>
475-
<td><input name="email" type="<?php echo esc_attr( $type ); ?>" id="adduser-email" class="wp-suggest-user" value="" /></td>
492+
<td><input name="email" type="<?php echo esc_attr( $type ); ?>" id="adduser-email" class="wp-suggest-user" value="<?php echo esc_attr( $adduser_email ); ?>" /></td>
476493
</tr>
477494
<tr class="form-field">
478495
<th scope="row"><label for="adduser-role"><?php _e( 'Role' ); ?></label></th>
479496
<td><select name="role" id="adduser-role">
480-
<?php wp_dropdown_roles( get_option( 'default_role' ) ); ?>
497+
<?php
498+
$adduser_role_default = $adduser_role ? $adduser_role : get_option( 'default_role' );
499+
wp_dropdown_roles( $adduser_role_default );
500+
?>
481501
</select>
482502
</td>
483503
</tr>
484504
<?php if ( current_user_can( 'manage_network_users' ) ) { ?>
485505
<tr>
486506
<th scope="row"><?php _e( 'Skip Confirmation Email' ); ?></th>
487507
<td>
488-
<input type="checkbox" name="noconfirmation" id="adduser-noconfirmation" value="1" />
508+
<input type="checkbox" name="noconfirmation" id="adduser-noconfirmation" value="1" <?php checked( $adduser_noconfirmation, '1' ); ?> />
489509
<label for="adduser-noconfirmation"><?php _e( 'Add the user without sending an email that requires their confirmation' ); ?></label>
490510
</td>
491511
</tr>

0 commit comments

Comments
 (0)