I have been trying to add new wordpress roles and capabilities, in a multisite installation, using the code below. The issue is, it only applies to the ‘main’ site of the multisite, and does not propogate to the subsites. I haven’t really found anything in the documentation that covers this.
function civicrm_wp_set_capabilities() {
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
//Minimum capabilities (Civicrm permissions) arrays
$min_capabilities = array(
'access_civimail_subscribe_unsubscribe_pages' => 1,
'access_all_custom_data' => 1,
'access_uploaded_files' => 1,
'make_online_contributions' => 1,
'profile_create' => 1,
'profile_edit' => 1,
'profile_view' => 1,
'register_for_events' => 1,
'view_event_info' => 1,
'sign_civicrm_petition' => 1,
'view_public_civimail_content' => 1,
);
// Assign the Minimum capabilities (Civicrm permissions) to all WP roles
foreach ( $wp_roles->role_names as $role => $name ) {
$roleObj = $wp_roles->get_role($role);
foreach ($min_capabilities as $capability_name => $capability_value) {
$roleObj->add_cap($capability_name);
}
}
//Add the 'anonymous_user' role with minimum capabilities.
if (!in_array('anonymous_user' , $wp_roles->roles)) {
add_role(
'anonymous_user',
'Anonymous User',
$min_capabilities
);
}
}
This is my experience.
I had to add role for each site in WordPress, I developed an add page in dashboard so the site administrator can add the self-defined role/capabilities.
But I found the
$wp_roles->add_role
,add_cap
only works the subsite. So I did some fix,I’ve made a plugin, the super-admin(not site administrator but the “admin”) can “Network Activate” in dashboard
the self-defined role/capabilities was saved in
role.ini
file, the plugin can create two table namedwp_s_role
andwp_s_cap
, The pre-self-defined role/capabilities will insert into the table.Then the
$wp_roles->add_rol
e andadd_cap
can across the network and insert the role/capabilities to all subsites.But the subsite administrator must add the self-defined role/capabilities and make it works in all subsites, so I create a trigger for
Then the uninstall function works, all the two tables data will be saved into the
role.ini
file again, after that, the install function will works again. and all self-defined role/capabilities was added into all subsites.As you see, I made a trigger in order to add the self-defined role into all subsites, but the effectivity is very low, the plugin restart will take more than 5 seconds. So I’ve improve the methods, This time, when the add_role function is done, I copied the subsite role/capabilities into the others subsites.
Some steps:
In subsites, when the administrator add the role, I use
$blog_id
and$table_prefix
to get a subsite tablewp_2_options
contents. (We assumed the blog_id is2
and the table_prefix iswp
).I select the
option_name=wp_2_user_roles
then I
foreach
the blogs and get the subsite table. So I insert the select result into each subsite table(wp_n_options
) and the main table(wp_options
), muhaha, am I smart enough? : )The below function is how to remove the self-defined roles in all subsites. I think it’s would helpful with you.
For the $site_result, I have another function to fetch all exact site info.
I have just finished the function and it works perfectly. You can use s_copy_site_role_cap() to call. The function can copy the role into others subsite roles after you add_role. Because the plugin might works in main site or subsite, there will have two ways in network(subdomain, subpath) , so i made another function to get the right current blog_name , so I can get the lastest roles contents from the blog_name information.
Thanks.
Just add it as a mu-plugin:
wp-content/mu-plugins/roles.php
mu-plugins are loaded by default on all sites on the network.