add_action('admin_post_edit_user_profile',function(){// Verify user is logged inif(!is_user_logged_in()){ wp_die('Unauthorized access');}// Verify nonceif(!isset($_POST['profile_edit_nonce'])||!wp_verify_nonce($_POST['profile_edit_nonce'],'edit_profile_action')){ wp_die('Security verification failed');}$current_user_id= get_current_user_id();$current_user= get_userdata($current_user_id);// Sanitize inputs$new_display_name=isset($_POST['edit-profile-data-name'])? sanitize_text_field($_POST['edit-profile-data-name']):'';$new_email=isset($_POST['edit-profile-data-email'])? sanitize_email($_POST['edit-profile-data-email']):'';$new_phone=isset($_POST['edit-profile-data-phone'])? sanitize_text_field($_POST['edit-profile-data-phone']):'';// Update display name if changedif($new_display_name&&$new_display_name!==$current_user->display_name){ wp_update_user(array('ID'=> $current_user_id,'display_name'=> $new_display_name));}// Update phone numberif($new_phone){ update_user_meta($current_user_id,'phone_no',$new_phone);}// Handle email change - initiate confirmation if changedif($new_email&&$new_email!==$current_user->user_email){// Validate email formatif(!is_email($new_email)){ wp_die('Invalid email address');}// Check if email already exists for another userif(email_exists($new_email)&& email_exists($new_email)!==$current_user_id){ wp_die('Email address already in use');}// Generate hash for confirmation$hash=md5($new_email.time(). wp_rand());// Store pending email change update_user_meta($current_user_id,'_new_email',array('hash'=> $hash,'newemail'=> $new_email));// Send confirmation email$sitename= wp_specialchars_decode(get_option('blogname'),ENT_QUOTES);$confirmation_url= add_query_arg('newuseremail',$hash, home_url('/visitors/'));$email_text=sprintf( __('Hello %s,You recently requested to change the email address on your account. If you made this request, please click the following link to confirm:%sYou can safely ignore this email if you do not want to make this change.Regards,%s%s','textdomain'),$current_user->user_login,$confirmation_url,$sitename, home_url()); wp_mail($new_email,sprintf(__('[%s] Email Change Request','textdomain'),$sitename),$email_text);}// Redirect back to profile page wp_redirect(home_url('/visitors/'),303);exit;});
PHP
This code snippet is used in the following tutorial(s):
This website uses cookies to enhance your experience. Some are essential for site functionality, while others help us analyze and improve your usage experience. Please review your options and make your choice.
If you are under 16 years old, please ensure that you have received consent from your parent or guardian for any non-essential cookies.
Your privacy is important to us. You can adjust your cookie settings at any time. For more information about how we use data, please read our privacy policy. You may change your preferences at any time by clicking on the settings button below.
Note that if you choose to disable some types of cookies, it may impact your experience of the site and the services we are able to offer.
Essential cookies and services enable basic functions and are necessary for the proper functioning of the website. These cookies and services do not require user permission according to GDPR.
These cookies and services are necessary for the proper functioning of the website, but their use requires user consent. These may include, but are not limited to: payment gateways, captcha services, embedded booking services.
This category includes all cookies, domains, and services that do not fall into the other specified categories or have not been explicitly categorized.
This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.