Disabling Email Changes – Not Working
-
Hello,
I’m working on some code to prevent users from changing their email address and name. We authenticate with an external source and profile data is managed by and synced from the external source.
I have written code to disable the HTML form fields on profile.php and user-edit.php. This is working fine. However, I also want to handle the POST data properly, as we all know the HTML form fields can be re-enabled with a DOM editor.
I was successful with preventing changes to first name, last name, and nickname, but email is not working. If I POST a new email address, I get the notice “There is a pending change of your email to [email protected]” and the email change confirmation email is sent out to the new address.
My code is below. I’d be very grateful for any assistance on preventing users from changing their email address. Thanks!
<?php new CustomStopEmailChange(); class CustomStopEmailChange { public function __construct() { add_action('admin_init', array($this, 'add_admin_hooks')); } public function add_admin_hooks() { // These two functions disable the actual HTML form fields using jquery. add_action('admin_footer-profile.php', array($this, 'disable_profile_fields')); add_action('admin_footer-user-edit.php', array($this, 'disable_profile_fields')); // Handle the POST data as well. add_action('personal_options_update', array($this, 'disable_profile_fields_post')); } public function disable_profile_fields() { $Selectors = '#first_name,#last_name,#nickname,#email'; // Use readonly instead of disabled because disabled fields are not included in the POST data. echo '<script type="text/javascript">jQuery(function() {' . "\n"; echo 'jQuery("' . $Selectors . '").prop("readonly", true);' . "\n"; echo '});</script>'; } public function disable_profile_fields_post() { // All of these work EXCEPT for the last one: email changes. add_filter('pre_user_first_name', function () { $UserObj = wp_get_current_user(); return $UserObj->first_name; }); add_filter('pre_user_last_name', function () { $UserObj = wp_get_current_user(); return $UserObj->last_name; }); add_filter('pre_user_nickname', function () { $UserObj = wp_get_current_user(); return $UserObj->user_nicename; }); add_filter('pre_user_email', function () { $UserObj = wp_get_current_user(); return $UserObj->user_email; }); } } ?>
- The topic ‘Disabling Email Changes – Not Working’ is closed to new replies.