WPMU Roles and Capabilities Hacks

Posted on April 28, 2010. Updated May 26, 2010.

Summary

Modifications you can use to create site-wide roles and capabilities in your Worpress Mu blog network.

The following are some quick hacks you can apply to the WordPress Capabilities Manager plugin to set up custom roles on your entire network.

As of this writing, the plugin could only create custom roles on a blog level. There has been lots of discussion on the matter. The root of the problem is that each blog in a WPMU installation has its own capabilities stored separately.

I initially tried to create my own database table with custom roles and capabilities, but I soon realized that WordPress would require a great deal more customization to make this work. So, I decided to give in and store all the data the "native" way (which I consider somewhat dirty).

The following are modifications made to the includes/manager.php included with the capsman plugin. In short, I replaced all the CRUD functions with some quick loops that iterate through each blog in the network, making use of WPMU's get_blog_list() function.

Caveats:

1. This has only been tested on a simple level using WPMU 2.9.2.
2. Due to the nature of WPMU, it creates a lot of duplicate data in your database.
3. Performance could be better, but it crunched through 1200 blogs in about 10 seconds.

Let's take a look at includes/manager.php.

On Line 292, replace saveRoleCapabilities with the following foreach loop. I commented out the original code that was there, leaving it for reference.

// *********** begin paul wenzel's modifications...
// updates/saves capabilties for this role, for all blogs
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
	switch_to_blog($blog['blog_id']);
	$this->saveRoleCapabilities($post['current'], $post['caps'], $post['level']);
}
// $this->saveRoleCapabilities($post['current'], $post['caps'], $post['level']); // Original Code
// ********* end paul wenzel's modifications

Around line 311, modify the call to createRole.

// *********** begin paul wenzel's modifications... create roles for all blogs
// TODO: Add WPMU/multisite check using function_exists(get_bloglist()).
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
	switch_to_blog($blog['blog_id']);
	$newrole = $this->createRole($post['create-name']);
}

// **** Original Code
// if ( $newrole = $this->createRole($post['create-name']) ) {
// 	ak_admin_notify(__('New role created.', $this->ID));
// 	$this->current = $newrole;
// } else {
// 	ak_admin_error(__('Error: Failed creating the new role.', $this->ID));
// }

// ********* end paul wenzel's modifications

On Line 335, modify the createRole call, copying the current role to a new one.

// *********** begin paul wenzel's modifications... updates roles for all blogs
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
	switch_to_blog($blog['blog_id']);
	$newrole = $this->createRole($post['copy-name'], $current->capabilities);
}

// *** Original Plugin Code
// if ( $newrole = $this->createRole($post['copy-name'], $current->capabilities) ) {
// 	ak_admin_notify(__('New role created.', $this->ID));
// 	$this->current = $newrole;
// } else {
// 	ak_admin_error(__('Error: Failed creating the new role.', $this->ID));
// }
// ********* end paul wenzel's modifications

On Line 362, add_cap method, adding a new capability to all blogs.

// *********** begin paul wenzel's modifications... adds a new capability
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
	switch_to_blog($blog['blog_id']);
	$role->add_cap($newname['name']);
}
// restore_current_blog();
// $role->add_cap($newname['name']); // Original Code
// ********* end paul wenzel's modifications

Finally, at Line 446, repeat the remove_role call, which deletes roles for all blogs.

// *********** begin paul wenzel's modifications... deletes roles for all blogs
$blog_list = get_blog_list( 0, 'all' );
foreach ($blog_list AS $blog) {
	switch_to_blog($blog['blog_id']);
	remove_role($this->current);
}

// remove_role($this->current); // original code
// ********* end paul wenzel's modifications

With the above iterative loops in place, you should be able to enjoy custom roles and capabilities throughout your blog network.


Comments

Comments are closed.