WordPress: How to create a new role in a plugin

Some notes to remember how to create a special functions in a plugin that can only be seen by a user with that role only.

Note this saves to the database, so add it in your plugin activation codes:

$result = add_role(
   __( 'Fancy Role Name' ),
     'my_custom_capacity' => true,
     'read' => true
$role = get_role( 'administrator' );
$role->add_cap( 'my_custom_capacity' );

Note I also add it into admin role, this is so the admins will also be able to access this.

Then my menus look like this:

add_menu_page( 'Menu Name', 'Menu Name', 'my_custom_capacity', 'plugin_name/plugin_name.php', '', 'http://www.mysite.com/favicon.ico', 67 );

Leave a Reply

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