Changing the Divi Role Editor Defaults

Written by Dan Mossop

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.

Run PHP Code Directly in your Divi Layouts

Unlock endless customization, automation, and dynamic functionality by seamlessly adding PHP code to your Divi pages and posts with the Divi PHP Code Module. Style, preview, and debug your PHP creations directly in the visual builder with robust error handling and enhanced security.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

2 Comments

  1. You need to add the ability to make Divi AI off by default. What would be great is a way to do that across many sites in one go… Agencies REALLY need this ability.

    Reply
    • Hey Concerned Citizen, I've just put up a post which modifies the code in this post to specifically give some control over the Divi AI role editor option, namely allowing you to disable it by default for non-administrators, disable it by default for administrators, and / or keep it permanently disabled:

      Disable Divi AI by default in the Divi Role Editor

      I hope it helps!

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

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

We may earn a commission when you visit links on our website.