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:

Spread the love

del.icio.us:Making custom user roles in Wordpress digg:Making custom user roles in Wordpress spurl:Making custom user roles in Wordpress wists:Making custom user roles in Wordpress simpy:Making custom user roles in Wordpress newsvine:Making custom user roles in Wordpress blinklist:Making custom user roles in Wordpress furl:Making custom user roles in Wordpress reddit:Making custom user roles in Wordpress fark:Making custom user roles in Wordpress blogmarks:Making custom user roles in Wordpress Y!:Making custom user roles in Wordpress smarking:Making custom user roles in Wordpress magnolia:Making custom user roles in Wordpress segnalo:Making custom user roles in Wordpress gifttagging:Making custom user roles in Wordpress

RSS feed | Trackback URI

1 Comment »

Comment by Leslie
2007-08-30 04:01:03

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?

 

Pingbacks and Trackbacks

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.