Stop Users From Changing Their Email Address
Makes the email field read-only for everyone except administrators, for sites where the email is tied to a contract or a login policy.
Opens WPIDE on
Code
add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) {
if ( ! $update || current_user_can( 'manage_options' ) || empty( $user->ID ) ) {
return;
}
$current = get_userdata( $user->ID );
if ( $current && isset( $user->user_email ) && strtolower( $user->user_email ) !== strtolower( $current->user_email ) ) {
$user->user_email = $current->user_email;
$errors->add( 'wpide_email_locked', '<strong>Error:</strong> Your email address can only be changed by an administrator.' );
}
}, 10, 3 );
add_action( 'admin_footer', function () {
$screen = get_current_screen();
if ( ! $screen || 'profile' !== $screen->id || current_user_can( 'manage_options' ) ) {
return;
}
echo '<script>(function(){var f=document.getElementById("email");if(f){f.readOnly=true;f.insertAdjacentHTML("afterend","<p class=\"description\">Contact an administrator to change your email address.</p>");}})();</script>';
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.