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

How to use editable_roles filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 16, 2023
5 minutes read

editable_roles filter

Filters the list of editable roles.

To use the editable_roles filter, you first need to register it using add_filter. You can include this code in the functions.php file of your active theme or in a custom WordPress plugin.

We at WePlugins always recommend creating a custom WordPress plugin when using hooks, so nothing breaks when you update your WordPress theme in the future.

In the examples below, we define a function modify_editable_roles_defaults that takes one parameter, and we register it using add_filter. The first parameter, editable_roles, is the name of the hook. The second parameter, modify_editable_roles_defaults, is the name of the function 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 to the registered function.

Sometimes, you need to remove a registered hook, so you can use remove_filter to remove the editable_roles filter.

Parameters

Below, the 1 parameter is required to use this hook.

  • $all_roles: (array[]) Array of arrays containing role information.

Live Example 1: Remove Higher Levels

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

    add_filter('editable_roles', 'weplugins_remove_higher_levels');
    function weplugins_remove_higher_levels($all_roles) {
        $user = wp_get_current_user();
        $next_level = 'level_' . ($user->user_level + 1);
        foreach ($all_roles as $name => $role) {
            if (isset($role['capabilities'][$next_level])) {
                unset($all_roles[$name]);
            }
        }
        return $all_roles;
    }
    

Live Example 2: Modify Editable Roles Defaults

How to modify the editable roles according to your website requirements.

    function weplugins_modify_editable_roles_defaults($all_roles) {
        // Update the $all_roles variable according to your website requirements and return this variable.
        // You can modify the $all_roles variable conditionally too if you want.
        return $all_roles; 
    }
    // add the filter
    add_filter("editable_roles", "weplugins_modify_editable_roles_defaults", 10, 1);
    

Live Example 3: Removing a Hook Callback

To remove a hook callback, use the example below.

    remove_filter("editable_roles", "weplugins_modify_editable_roles_defaults", 10, 1);
    

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

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, please contact us and we’d be happy to assist you.

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.