Update user_email via php - WP 4.7
I am brand new to php and web development. I've made a front-end edit profile page for users, and every field updates except email (but, everything else is user meta). I have spent way too long on this to no avail.
There are lots of posts about this, such as this one How do you update user_email on the front end in WP 3.3?, but I cannot get any of the methods suggested to work for me. I am using WP 4.7.3.
This is from the above post. It DOES update the email, but my page hangs indefinitely after I submit my form:
wp_update_user( array( 'ID' = $current_user-ID, 'user_email' = $_POST['email'] ) );
I also tried this way. It does not update the user's email, and it doesn't hang:
$wpdb-update($wpdb-users, array('user_email' = $_POST['email']), array('ID' = $current_user-ID));
Any advice would be greatly appreciated. I am pretty sure the issue lies just in this piece of code, but I can include more code if that helps.
Edited: update user profile code. Turns out password hangs indefinitely too.
if ( 'POST' == $_SERVER['REQUEST_METHOD'] !empty( $_POST['action'] ) $_POST['action'] == 'update-user' ) {
/* Update user information. */
// Email
if (isset( $_POST['email'])) {
// check if user is really updating the value
if ($user_email != $_POST['email']) {
// check if email is free to use
if (email_exists( $_POST['email'] )){
// Email exists, do not update value.
$error[] = __('This email is already in use.', 'profile');
}
else if (!is_email( $_POST['email'] )){
// Not correct email format.
$error[] = __('Email is in incorrect format.', 'profile');
}
else {
wp_update_user( array( 'ID' = $current_user-ID, 'user_email' = $_POST['email'] ) );
//$wpdb-update($wpdb-users, array('user_email' = $_POST['email']), array('ID' = $current_user-ID));
}
}
}
// Password
if ( !empty($_POST['pass1'] ) !empty( $_POST['pass2'] ) ) {
if ( $_POST['pass1'] == $_POST['pass2'] )
wp_update_user( array( 'ID' = $current_user-ID, 'user_pass' = esc_attr( $_POST['pass1'] ) ) );
else {
$error[] = __('Passwords do not match.', 'profile');
}
}
// First name
if ( !empty( $_POST['first-name'] ) ){
update_user_meta( $current_user-ID, 'first_name', esc_attr( $_POST['first-name'] ) );
}
// Last name
if ( !empty( $_POST['last-name'] ) )
update_user_meta($current_user-ID, 'last_name', esc_attr( $_POST['last-name'] ) );
// Birth year
if ( !empty( $_POST['birth_year'] ) ) {
// Make sure it's all numbers
if (!ctype_digit($_POST['birth_year'])){
$error[] = __('Please enter a year.', 'profile');
}
else
update_user_meta($current_user-ID, 'birth_year', esc_attr( $_POST['birth_year'] ) );
}
// Phone
if ( !empty( $_POST['phone'] ) ) {
update_user_meta($current_user-ID, 'phone', esc_attr( $_POST['phone'] ) );
}
// Street address
if ( !empty( $_POST['address'] ) )
update_user_meta($current_user-ID, 'street_address', esc_attr( $_POST['address'] ) );
// City
if ( !empty( $_POST['city'] ) )
update_user_meta($current_user-ID, 'city', esc_attr( $_POST['city'] ) );
// State
if ( !empty( $_POST['state'] ) )
update_user_meta($current_user-ID, 'state', esc_attr( $_POST['state'] ) );
// Parent's first name
if ( !empty( $_POST['p_first_name'] ) )
update_user_meta($current_user-ID, 'player_parent_first_name', esc_attr( $_POST['p_first_name'] ) );
// Parent's last name
if ( !empty( $_POST['p_last_name'] ) )
update_user_meta($current_user-ID, 'player_parent_last_name', esc_attr( $_POST['p_last_name'] ) );
// Parent's email
if (isset( $_POST['p_email'])) {
// check if user is really updating the value
if ($user_email != $_POST['p_email']) {
if (!is_email( $_POST['p_email'] )){
// Not correct email format.
$error[] = __('Parent email is in incorrect format.', 'profile');
}
else {
$args = array(
'ID' = $current_user-id,
'player_parent_email' = esc_attr( $_POST['p_email'] )
);
wp_update_user( $args );
}
}
}
// Parent's phone
if ( !empty( $_POST['p_phone'] ) ) {
update_user_meta($current_user-ID, 'player_parent_phone', esc_attr( $_POST['p_phone'] ) );
}
/* Redirect so the page will show updated info.*/
// Make sure there are no errors
if ( count($error) == 0 ) {
echo "redirecting";
//action hook for plugins and extra fields saving
do_action('edit_user_profile_update', $current_user-ID);
wp_redirect( get_permalink().'?updated=true' );
exit;
}
else {
// print errors here
}
}