Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use email_change_email filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
August 13, 2022
5 minutes read

email_change_email filter

Filters the contents of the email sent when the user’s email is changed.

To use the email_change_email filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function weplugins_modify_email_change_email_defaults which takes 3 parameters and we registered it using add_filter. The first parameter email_change_email is the name of the hook, the second parameter weplugins_modify_email_change_email_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook, so you can use remove_filter to remove the email_change_email filter.

Parameters

    Below are the 3 parameters required to use this hook.

  • $email_change_email: (array) Used to build wp_mail().
    • ‘to’ (string) The intended recipients.
    • ‘subject’ (string) The subject of the email.
    • ‘message’ (string) The content of the email. The following strings have a special meaning and will get replaced dynamically:
      • ###USERNAME### The current user’s username.
      • ###ADMIN_EMAIL### The admin email in case this was unexpected.
      • ###NEW_EMAIL### The new email address.
      • ###EMAIL### The old email address.
      • ###SITENAME### The name of the site.
      • ###SITEURL### The URL to the site.
    • ‘headers’ (string) Headers.
  • $user: (array) The original user array.
  • $userdata: (array) The updated user array.

Live Example

apply_filters('email_change_email', array $email_change_email, array $user, array $userdata)

Below is an example of how you can use this hook.

Example 1: Basic Usage

This example shows how to modify the content of the email sent when a user’s email is changed.

    function weplugins_modify_email_change_email_defaults($email_change_email, $user, $userdata) { 
        // Update the $email_change_email variable according to your website requirements and return this variable.
        // You can modify the $email_change_email variable conditionally too if you want.
        return $email_change_email; 
    }
    // add the filter
    add_filter("email_change_email", "weplugins_modify_email_change_email_defaults", 10, 3);
    

Example 2: Conditional Modification

This example shows how to conditionally modify the email content based on the user’s role.

    function weplugins_modify_email_change_email_defaults_conditional($email_change_email, $user, $userdata) { 
        if (in_array('administrator', $user['roles'])) {
            $email_change_email['subject'] = 'Admin Email Change Notification';
        }
        return $email_change_email; 
    }
    // add the filter
    add_filter("email_change_email", "weplugins_modify_email_change_email_defaults_conditional", 10, 3);
    

Example 3: Removing the Hook

To remove a hook callback, use the example below.

    remove_filter("email_change_email", "weplugins_modify_email_change_email_defaults", 10, 3);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook and need customization, please contact us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.