Allow User to Edit Specific Post Using Map_Meta_Cap()

WordPress

In the event a client or user needs to have access to edit only the contents of a specific post, page or other custom post type without having access to edit any other posts of that type.

Say for example we have a single page in our site called ‘About Us’ which the client would like to edit, but we don’t want them to be able to edit or see our other pages from inside the admin area.

We can achieve this using the map_meta_cap filter.

You can do this first part with a role management plugin, I recommend the members plugin by Justin Tadlock.

Add Custom Capability

Firstly, add a new capability to your site (you can also add this using your role management plugin).

You’ll need to make sure this capability is checked for your desired role as well as the administrator.

Also, as this saves the new capability to the database this function is possibly best hooked to the plugin or theme activation.

Map Meta Capabilities for About Page

For this we’ll need to know the post id of the post we’re giving access to. You can find this in the url on the edit screen for that post after post.php?post=.

This function simply checks to see if the current action being taken is editing our specific post and if the current users role has our required capability. If so, make this capability our only required capability. You could leave this blank, but lets not.

Add Menu Page for Admin Access

Currently the user can now only access this edit screen from the edit link when viewing the page itself. Assuming that the theme has this built in.

A nicer feature would be if we just added a new menu item which will take the user straight to this posts edit screen, therefore retaining the look and feel of the regular WordPress admin user interface and making the experience more intuitive for your client/user.

One thought on “Allow User to Edit Specific Post Using Map_Meta_Cap()

  1. Hello, I can’t get this scripts to work. I use this code below, but the role “calendar_editor” can still edit all pages…

Leave a Reply

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