Users & Login PHP Intermediate

Stop Users Changing Their Own Email Address

Only let administrators change a user's email address, so members and customers can't switch their account to a different email.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 17 lines
add_action( 'user_profile_update_errors', function ( WP_Error $errors, bool $update, stdClass $user ): void {
	if ( ! $update || current_user_can( 'manage_options' ) ) {
		return;
	}

	$current = get_userdata( $user->ID );
	if ( $current && isset( $user->user_email ) && strtolower( $user->user_email ) !== strtolower( $current->user_email ) ) {
		$errors->add( 'nahnu_email_locked', 'Please contact us to change your email address.' );
	}
}, 10, 3 );

add_action( 'admin_print_footer_scripts-profile.php', function (): void {
	if ( current_user_can( 'manage_options' ) ) {
		return;
	}
	echo "<script>document.getElementById( 'email' )?.setAttribute( 'readonly', 'readonly' );</script>";
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

On membership and B2B sites, the email address is often tied to a contract, licence or billing account. Letting users change it themselves can break those links. This snippet locks the field for everyone except administrators.

How it works

user_profile_update_errors runs when a profile is saved, before anything is written. If a non-administrator submits a different email, the snippet adds an error and WordPress refuses the save.

A small script also makes the Email field read-only on the profile screen, so people see straight away that they can’t change it.

Things to watch

  • This covers the WordPress profile screen. WooCommerce’s My Account → Account details form saves through its own code, so lock that separately if needed.
  • Administrators can still change any user’s email.

How to test it

Log in as a subscriber, open your profile and check the Email field is read-only. Changing it with browser tools and saving should show the error.

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.

Accept All Accept Required Only