Add Role

This simple plugin, built for my WordPress DC Presentation on the Users API adds a role called link master, that gets the capability to edit links and also read private posts and pages and also removes the ability for editors to manage categories.

This plugin only needs to run once, and in fact to make sure it doesn’t run the actual adding or roles multiple times, we are going to use an option to prevent it from happening.

class jorbin_role_master{

	/**
	 * Add our init action
	 */
	function __construct(){
		add_action('init', array($this, 'init') );

	}

We start by creating our class, and when it is initialized add an action to the WordPress init hook.

	/**
	 * Check to see if we need to do anything
	 *
	 */
	function init(){
		if ( FALSE === get_option('jorbin_role_master') ):
			$this->populate_role_1();
			add_option('jorbin_role_master', '1', '', 'yes');
		endif;
	}

On the init hook, we check to see if our option exists. If it doesn’t, we call our method that add the roles and then add an option to prevent the role from getting added over and over again.

	/**
	 * Initial role population for link master and strip editor of manage cats
	 *
	 */
	function populate_role_1(){
		add_role('link_master', 'Link Master');

		$role =& get_role('link_master');
		$role->add_cap('read');
		$role->add_cap('manage_links');
		$role->add_cap('read_private_posts');
		$role->add_cap('read_private_pages');

		$role =& get_role('editor');
		$role->remove_cap('manage_categories');

	}

}

$jorbin_role_master = new jorbin_role_master();

Our method that actually calls the option first adds the role, the loads it into a variable where we can call add_cap to add the specific capabilities we want. After that, we remove the manage_categories capability from the editor. After we create a new object with our class, we are good to go. Easy. As. Pie.

About Aaron Jorbin

Aaron Jorbin is an engineer with Clearspring where he works on AddThis and a WordPress Core Contributor. He has spoken to multiple User Groups and at WordCamps in four time zones. When he's not busy creating and fixing bugs, Aaron helps run an educational simulation conference for over 1500 college students. He'll gladly toast to the GPL any day of the week and happily will discuss whisky, quality beer, or the upper peninsula of Michigan with anyone that wants.
This entry was posted in Code, Programming, WordPress and tagged , , . Bookmark the permalink.

Leave a Reply

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

* * . Now that I have read Add Role, I wanted to say:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">