edit_user

Edit user settings based on contents of $_POST.

Description

(int|WP_Error) edit_user( (int) $user_id = 0 ); 

Used on user-edit.php and profile.php to manage and process user options, passwords etc.

Returns (int|WP_Error)

user id of the updated user

Parameters (1)

0. $user_id — Optional. (int)
User ID.

Usage

  1. if ( !function_exists( 'edit_user' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/user.php'; 
  3.  
  4. // Optional. User ID. 
  5. $user_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = edit_user($user_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/user.php  
  1. function edit_user( $user_id = 0 ) { 
  2. $wp_roles =wp_roles); 
  3. $user = new stdClass; 
  4. if ( $user_id ) { 
  5. $update = true; 
  6. $user->ID = (int) $user_id; 
  7. $userdata = get_userdata( $user_id ); 
  8. $user->user_login = wp_slash( $userdata->user_login ); 
  9. } else { 
  10. $update = false; 
  11.  
  12. if ( !$update && isset( $_POST['user_login'] ) ) 
  13. $user->user_login = sanitize_user($_POST['user_login'], true); 
  14.  
  15. $pass1 = $pass2 = ''; 
  16. if ( isset( $_POST['pass1'] ) ) 
  17. $pass1 = $_POST['pass1']; 
  18. if ( isset( $_POST['pass2'] ) ) 
  19. $pass2 = $_POST['pass2']; 
  20.  
  21. if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) { 
  22. $new_role = sanitize_text_field( $_POST['role'] ); 
  23. $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false; 
  24. // Don't let anyone with 'edit_users' (admins) edit their own role to something without it. 
  25. // Multisite super admins can freely edit their blog roles -- they possess all caps. 
  26. if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) ) 
  27. $user->role = $new_role; 
  28.  
  29. // If the new role isn't editable by the logged-in user die with error 
  30. $editable_roles = get_editable_roles(); 
  31. if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) ) 
  32. wp_die(__('You can’t give users that role.')); 
  33.  
  34. if ( isset( $_POST['email'] )) 
  35. $user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) ); 
  36. if ( isset( $_POST['url'] ) ) { 
  37. if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) { 
  38. $user->user_url = ''; 
  39. } else { 
  40. $user->user_url = esc_url_raw( $_POST['url'] ); 
  41. $protocols = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) ); 
  42. $user->user_url = preg_match('/^(' . $protocols . '):/is', $user->user_url) ? $user->user_url : 'http://'.$user->user_url; 
  43. if ( isset( $_POST['first_name'] ) ) 
  44. $user->first_name = sanitize_text_field( $_POST['first_name'] ); 
  45. if ( isset( $_POST['last_name'] ) ) 
  46. $user->last_name = sanitize_text_field( $_POST['last_name'] ); 
  47. if ( isset( $_POST['nickname'] ) ) 
  48. $user->nickname = sanitize_text_field( $_POST['nickname'] ); 
  49. if ( isset( $_POST['display_name'] ) ) 
  50. $user->display_name = sanitize_text_field( $_POST['display_name'] ); 
  51.  
  52. if ( isset( $_POST['description'] ) ) 
  53. $user->description = trim( $_POST['description'] ); 
  54.  
  55. foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) { 
  56. if ( isset( $_POST[$method] )) 
  57. $user->$method = sanitize_text_field( $_POST[$method] ); 
  58.  
  59. if ( $update ) { 
  60. $user->rich_editing = isset( $_POST['rich_editing'] ) && 'false' == $_POST['rich_editing'] ? 'false' : 'true'; 
  61. $user->admin_color = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh'; 
  62. $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false'; 
  63. $user->locale = ''; 
  64.  
  65. if ( isset( $_POST['locale'] ) ) { 
  66. $locale = sanitize_text_field( $_POST['locale'] ); 
  67. if ( 'site-default' === $locale ) { 
  68. $locale = ''; 
  69. } elseif ( '' === $locale ) { 
  70. $locale = 'en_US'; 
  71. } elseif ( ! in_array( $locale, get_available_languages(), true ) ) { 
  72. $locale = ''; 
  73.  
  74. $user->locale = $locale; 
  75.  
  76. $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : ''; 
  77.  
  78. $user->use_ssl = 0; 
  79. if ( !empty($_POST['use_ssl']) ) 
  80. $user->use_ssl = 1; 
  81.  
  82. $errors = new WP_Error(); 
  83.  
  84. /** checking that username has been typed */ 
  85. if ( $user->user_login == '' ) 
  86. $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) ); 
  87.  
  88. /** checking that nickname has been typed */ 
  89. if ( $update && empty( $user->nickname ) ) { 
  90. $errors->add( 'nickname', __( '<strong>ERROR</strong>: Please enter a nickname.' ) ); 
  91.  
  92. /** 
  93. * Fires before the password and confirm password fields are checked for congruity. 
  94. * @since 1.5.1 
  95. * @param string $user_login The username. 
  96. * @param string &$pass1 The password, passed by reference. 
  97. * @param string &$pass2 The confirmed password, passed by reference. 
  98. */ 
  99. do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) ); 
  100.  
  101. // Check for blank password when adding a user. 
  102. if ( ! $update && empty( $pass1 ) ) { 
  103. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) ); 
  104.  
  105. // Check for "\" in password. 
  106. if ( false !== strpos( wp_unslash( $pass1 ), "\\" ) ) { 
  107. $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) ); 
  108.  
  109. // Checking the password has been typed twice the same. 
  110. if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) { 
  111. $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) ); 
  112.  
  113. if ( !empty( $pass1 ) ) 
  114. $user->user_pass = $pass1; 
  115.  
  116. if ( !$update && isset( $_POST['user_login'] ) && !validate_username( $_POST['user_login'] ) ) 
  117. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' )); 
  118.  
  119. if ( !$update && username_exists( $user->user_login ) ) 
  120. $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' )); 
  121.  
  122. /** This filter is documented in wp-includes/user.php */ 
  123. $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); 
  124.  
  125. if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) { 
  126. $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) ); 
  127.  
  128. /** checking email address */ 
  129. if ( empty( $user->user_email ) ) { 
  130. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) ); 
  131. } elseif ( !is_email( $user->user_email ) ) { 
  132. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ), array( 'form-field' => 'email' ) ); 
  133. } elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) { 
  134. $errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) ); 
  135.  
  136. /** 
  137. * Fires before user profile update errors are returned. 
  138. * @since 2.8.0 
  139. * @param WP_Error &$errors WP_Error object, passed by reference. 
  140. * @param bool $update Whether this is a user update. 
  141. * @param stdClass &$user User object, passed by reference. 
  142. */ 
  143. do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) ); 
  144.  
  145. if ( $errors->get_error_codes() ) 
  146. return $errors; 
  147.  
  148. if ( $update ) { 
  149. $user_id = wp_update_user( $user ); 
  150. } else { 
  151. $user_id = wp_insert_user( $user ); 
  152. $notify = isset( $_POST['send_user_notification'] ) ? 'both' : 'admin'; 
  153.  
  154. /** 
  155. * Fires after a new user has been created. 
  156. * @since 4.4.0 
  157. * @param int $user_id ID of the newly created user. 
  158. * @param string $notify Type of notification that should happen. See wp_send_new_user_notifications() 
  159. * for more information on possible values. 
  160. */ 
  161. do_action( 'edit_user_created_user', $user_id, $notify ); 
  162. return $user_id;