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.

[php]
class jorbin_role_master{

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

}
[/php]
We start by creating our class, and when it is initialized add an action to the WordPress init hook.
[php]
/**
* 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;
}
[/php]
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.
[php]
/**
* 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();
[/php]
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.

Leave a Reply

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