Add Snippet To Project
Are you looking to customize the access to menus and widgets on your WordPress site? By default, WordPress provides different user roles with various levels of access to these features. However, if you want to have more control over who can manage menus and widgets, you’ll need to change role access. In this article, we’ll guide you through the process of modifying user roles to grant or limit access to menus and widgets in WordPress.
function wpturbo_change_role_access() {
// Get the user role
$user_role = wp_get_current_user()->roles[0];
// Define the menu and widget access for each role
$access_settings = array(
'administrator' => array(
'menus' => array(
'edit_theme_options' // Grant full access to menus
),
'widgets' => array(
'edit_theme_options' // Grant full access to widgets
)
),
'editor' => array(
'menus' => array(
'edit_theme_options', // Grant access to certain menu capabilities for editors
'edit_pages',
'edit_posts',
'edit_others_posts',
'publish_posts'
),
'widgets' => array(
'edit_theme_options' // Grant full access to widgets
)
),
'author' => array(
'menus' => array(
'edit_theme_options' // Grant full access to menus
),
'widgets' => array(
'edit_theme_options', // Grant access to certain widget capabilities for authors
'edit_posts',
'edit_published_posts'
)
)
);
// Check if the user role has defined access settings
if (array_key_exists($user_role, $access_settings)) {
$menus_access = $access_settings[$user_role]['menus'];
$widgets_access = $access_settings[$user_role]['widgets'];
// Grant menu access for the user role
foreach ($menus_access as $capability) {
$role = get_role($user_role);
$role->add_cap($capability);
}
// Grant widget access for the user role
foreach ($widgets_access as $capability) {
$role = get_role($user_role);
$role->add_cap($capability);
}
}
}
add_action('init', 'wpturbo_change_role_access');
The code snippet provided aims to change the role-based access to menus and widgets in WordPress. By default, different user roles have different levels of access to menus and widgets in the WordPress admin dashboard. However, sometimes it is necessary to modify these access settings to fit specific requirements.
The wpturbo_change_role_access()
function is responsible for changing the access settings for menus and widgets based on the user role. The first step is to get the current user role using the wp_get_current_user()->roles[0]
function, which retrieves the first role assigned to the user.
Next, an associative array called $access_settings
is defined to map the specific menu and widget access for each user role. For example, the 'administrator'
role is granted full access to menus and widgets by assigning the 'edit_theme_options'
capability to them.
Within the array, each user role is defined as an array with two keys: 'menus'
and 'widgets'
. Under each key, an array of capabilities is assigned to specify the access level for that particular user role.
To change the access settings, the code loops through the capabilities assigned for the user role and adds them to the respective user role using the add_cap()
method. This allows users with that role to have the specified access capabilities.
Finally, the code checks if the user role has defined access settings in the $access_settings
array. If the user role exists in the array, the code retrieves the menus and widgets access settings assigned for that role. The menus access settings are added to the user role first, followed by the widgets access settings.
The add_action('init', 'wpturbo_change_role_access')
line hooks the wpturbo_change_role_access()
function into the init
action, ensuring that the role access settings are changed when WordPress initializes.
By using this code snippet and modifying the $access_settings
array, you can easily customize the access levels for different user roles in WordPress, granting or restricting their ability to access menus and widgets according to your specific requirements.