Changing the Divi Role Editor Defaults

Boost Your Site and Save 20% this Weekend

Join 50,000 users and build a better site with 20% off all Divi Booster plugins.

Discount applied at checkout. Offer ends Monday!

Day(s)

:

Hour(s)

:

Minute(s)

:

Second(s)

The Divi Theme comes with a role editor that lets you control which user roles (administrator, editor, contributor, etc) can use which Divi features. By default, everyone has access to everything and you must explicitly disable any features you don't want accessed by particular user roles. Today I was looking for a way to programmatically restrict a module so that, by default, it would only be enabled for administrators.

It would be easy to do this if there was a suitable filter on the role editor settings, but as yet there isn't. Instead, I was able to do it by directly altering the option which stores the role editor settings. Here's an example of my approach, in this case setting the login module to be enabled for the administrator only:

$role_setting = 'et_pb_login'; // module / role setting slug

// Get the role settings and list of roles which have already had defaults applied
$defaults_set = get_option($role_setting.'_defaulted_roles', array());
$et_pb_role_settings = get_option('et_pb_role_settings', array());

foreach($et_pb_role_settings as $role=>$settings) {
	if (!in_array($role, $defaults_set) && is_array($settings)) {
		
		// Enable the setting for the admin role only
		$state = ($role === 'administrator')?'on':'off';
		$et_pb_role_settings[$role][$role_setting] = $state;
		
		$defaults_set[] = $role;
	}
}

// Save the updated role settings and role default list
update_option($role_setting.'_defaulted_roles', $defaults_set);
update_option('et_pb_role_settings', $et_pb_role_settings);
The first time the code runs, it sets the named role setting to off for all but the administrator. It then records the roles it has affected, so that a) it doesn't change them in the future, and b) it can detect and set the default on any roles added in the future. By only modifying the value for a role once, it ensures that subsequent changes made by the administrator are not overwritten.

Want get more out of Divi?

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This post may contain referral links which may earn a commission for this site

Divi Booster

Hundreds of new features for Divi
in one easy-to-use plugin

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *