This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Ever wondered how to execute some custom code right after a user role is removed in WordPress? Let me introduce you to the remove_user_role action hook. This hook fires immediately after a role has been removed from a user. It’s super handy for a variety of custom functionalities!
To start using the remove_user_role action, you first need to register it using add_action. You can place this code in the functions.php file of your activated theme or, even better, in a custom WordPress plugin. At WePlugins, we always recommend creating a custom WordPress plugin to avoid breaking anything when you update your WordPress theme in the future.
In the examples below, we’ll walk you through defining a function called weplugins_execute_on_remove_user_role_event, which takes two parameters. We’ll then register this function using add_action. The parameters for add_action are the hook name, the function name, the priority, and the number of arguments.
Parameters
Below are the two parameters required to use this hook:
- $user_id : (int) The user ID.
- $role : (string) The removed role.
Live Example 1
Let’s see how you can use this hook in action.
function weplugins_execute_on_remove_user_role_event($user_id, $role) { // Custom code to execute when this action occurs in WordPress // Use the parameters received in the function arguments to implement the required custom functionality } // Add the action add_action("remove_user_role", "weplugins_execute_on_remove_user_role_event", 10, 2);
Live Example 2
Here’s how you might log the user ID and role to a custom log file whenever a role is removed:
function weplugins_log_removed_role($user_id, $role) { error_log("User ID: $user_id had role: $role removed.", 3, "/path/to/your/logfile.log"); } // Add the action add_action("remove_user_role", "weplugins_log_removed_role", 10, 2);
Live Example 3
If you need to notify an admin whenever a user role is removed, you can use the example below:
function weplugins_notify_admin_on_role_removal($user_id, $role) { $admin_email = get_option('admin_email'); wp_mail($admin_email, "Role Removed", "User ID: $user_id has had the $role role removed."); } // Add the action add_action("remove_user_role", "weplugins_notify_admin_on_role_removal", 10, 2);
Removing the Hook
To remove a hook callback, use the example below:
remove_action("remove_user_role", "weplugins_execute_on_remove_user_role_event", 10, 2);
Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
If you’re having any trouble using this hook or need customization, please contact our WordPress Development Team and we’d be happy to assist you.
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.