This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Create multiple WordPress roles and limited capabilities for Schools

Personally I have set up many school websites and out of the box WordPress has a pretty sophisticated user roles and capabilities authoring engine. This can pretty much apply to any multiple user website but by just considering schools we can break it down in to user chunks.

So, schools have; Students, Staff, Teachers, Deputy Heads and probably a Head who will have more capabilities than others. Using the following code in your functions.php file you can firstly add multiple generic roles then at a later stage give them more capabilities like so:


// add multiple roles with basic capabilities for later use
function add_multiple_roles() {
	$roles = array('head' => 'Head','dep_head' => 'Deputy Head','teacher' => 'Teacher','staff' => 'Staff','student'=>'Student');

	$capabilities = array(
	    'read' => true,  // true allows this capability
	);

	foreach ($roles as $k => $v) {
		add_role( $k, __( $v ),  $capabilities );
	}
}
add_action( 'admin_init', 'add_multiple_roles');

function add_staff_caps() {
    // only relates to staff
    $role = get_role( 'staff' );
	// edit their own posts
    $role->add_cap( 'edit_posts' );
}
add_action( 'admin_init', 'add_staff_caps');

function add_teacher_caps() {
    // only relates to teacher
    $role = get_role( 'teacher' );
	// delete their own posts
    $role->add_cap( 'delete_posts' );
	// edit their own posts
    $role->add_cap( 'edit_posts' );
}
add_action( 'admin_init', 'add_teacher_caps');

function add_dep_head_caps() {
    // only relates to dep_head
    $role = get_role( 'dep_head' );
 	// publish their own posts
    $role->add_cap( 'publish_posts' );
 	// delete their own posts
    $role->add_cap( 'delete_published_posts' );
 	// edit their own published posts
    $role->add_cap( 'edit_published_posts' );
	// edit their own posts
    $role->add_cap( 'edit_posts' );
}
add_action( 'admin_init', 'add_dep_head_caps');

function add_head_caps() {
   $role = get_role( 'head' );
   // do whatever you see fit for the Head to do.....
}
add_action( 'admin_init', 'add_head_caps');

At some point you’ll probably want to change these capabilities but without getting yourself lost down a rabbit hole it’s probably worth mentioning that these capabilities are added to the database and if you think by removing them from your functions.php it is going to work as you would expect – think again!

This is not the case so, to chop and change between capabilities and user roles you’ll have to unregister them or remove them per role it they have been added previously. To show this as an example I’ve commented out the line that previously switch the capability on, if it wasn’t removed from the role it would still retain the capability eg:

function add_head_caps() {
   $role = get_role( 'head' );
   // do whatever you see fit for the Head to do...
   // it was switched on previously and commenting it out will not switch it off...
   // $role->add_cap( 'publish_posts' );
   // if it was previously added then you need to remove it....
   $role->remove_cap( 'publish_posts' );
}
add_action( 'admin_init', 'add_head_caps');

if you get in to a quagmire of not knowing where you are with capabilities you can always debug with the following to see what is going on:

print_r($role = get_role( 'head' ));

About

A geek who likes to code and be nice ^_^

2 thoughts on “Create multiple WordPress roles and limited capabilities for Schools

  1. Really easy to follow, Elliott. I’ve typically used plugins to create new roles & capabilities, but this shows how easy it is to create a functionality plugin of our own. Good encouragement to dive in & get my hands dirty.

    The first comment under each add_caps function says “only relates to staff.” You might want to update that to reflect the role that you are adding caps to (just so no one gets confused). I’m assuming those should say “only relates to teacher” and “only relates to dep head.”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.