Making custom user roles in Wordpress

           I finally figured out how to hack the user roles in wordpress to control exactly what they could and couldn't do. Although the default user roles were adequate, it didn't provide the type of control I needed. Wordpress 2.0 assigns user roles with an associated user level for backwards compatibility. These access levels are labeled 0-10; 10 being the highest access level granted.

            So, exactly how do you customize these default user roles? The first thing to do is go to http://codex.wordpress.org/Roles_and_Capabilities and read the user level vs. role conversion details and brush up on the different user roles.

            Next, let's familiarize ourselves with exactly what happens when a user logs in to Wordpress. This is a very obtuse and basic overview of the process; please don't hold me accountable for details. When a user logs in they are redirected to the admin.php page. Once they enter their id and password admin.php calls other php files to check the user roles then calls menu.php to build the admin page and menu. The menu is built based on access levels (0-10). So if your profile is set as a level 7 and the "Write Page" button is set with a level 8; you can't see it. In a nutshell, we are going to alter this menu.php document to give us the custom access we want. Let's get busy and start coding!

Step 1: Go to your Wordpress root, then \wp-admin\menu.php. Open it in the editor of your choice.

Step 2: Now edit any menu item or sub-menu item according to different user levels. See below for my example.

Example: I had a user setup as with the role of "Editor", but, due to a custom Wordpress build I did not want the user creating, editing, or managing pages or categories. So I found the items I wanted to edit…

  • $submenu['post.php'][10] = array(__('Write Page'), ‘edit_pages', ‘page-new.php');
  • $submenu['edit.php'][15] = array(__('Categories'), ‘manage_categories', ‘categories.php')

Since the default Editor role has an access level of 7, I needed to change the level of these sub-menu items to 8 by adding the following lines of code:

  • $submenu['post.php'][10] = array(__('Write Page'), 8, ‘edit_pages', ‘page-new.php');
  • $submenu['edit.php'][15] = array(__('Categories'), 8, ‘manage_categories', ‘categories.php')

Save the file and copy it over ther old and now when the user logs in they no longer see the "Write Page", "Manage Page", or any Categories menu items. All done!

Related Articles:

6 Responses to “Making custom user roles in Wordpress”


  • Very cool. I see you wrote this a year ago, and WP has moved on since then, but I thought I’d write anyway. The thing I want is when I assign the “edit pages” capability, that the user doesn’t automatically see the “Write” tab and start getting all excited about writing tons of new pages. I do a lot of highly customised designs, and that kind of client interference invariably breaks the navigation.

    Hey, while I’m at it, do you know of any way to turn off all the font control buttons in the editor? I know it’s either FCK Editor or TinyMCE (can’t remember which one); have you ever done that?

  • Great information!

    Do you know if it is possible to limit the category a user can use? For instance, I am building a community in the weight-loss niche (same site connected to my name), and I want to allow my users to post about their own weight-loss journey via my blog under the category Community Blog … So I only want them to have the permission to post under that category ;)

    Thanks in advance,
    Joshua C.

  • @Joshua

    There is a plugin that lets you restrict specific users’ posts to a specific category: “Bind User to Category”. Until it’s updated, a small hack is needed to get this working with more recent versions of WP (see http://www.technokinetics.com/bind-user-to-category-wp-28/), but other than that it works well.

    Tim

  • There is a plugin that lets you restrict specific users’ posts to specific categories: “Bind User to Category”. A small hack is required to get this working with more recent versions of WP (see http://www.technokinetics.com/bind-user-to-category-wp-28/), but other than that my experience is that it works well.

  • can any one please help me out in creating different user levels?

  • Role Scoper will sort you out, without the need to hack files:

    http://wordpress.org/extend/plugins/role-scoper/

Leave a Reply