2

I am working with the Role and Capabilities API for the first time today and making use of the the excellent Members plugin. Having tinkered around for some time I notice that the "edit pages" capability does two things:

  1. Allow the user to create a new page
  2. Allow the user to edit existing pages

I am trying to split this capability into two. I want my user to:

  1. Not be able to create any new pages
  2. Allow the user to edit existing pages

I am looking to write some code for my theme that allows this to happen. I can create a new capability very easily. The challenge is then getting WordPress to respond accordingly to it.

Any thoughts would be appreciated.

2 Answers 2

1

Yes, this is indeed a problem. A dedicated 'create_posts' capability is planed:

http://core.trac.wordpress.org/ticket/16714

1
  • Thanks for the link, it is a shame this is not available now. As a temporary solution I have filtered the relevant menus from the admin menu and hidden certain buttons with CSS. Not ideal but it works for now.
    – Scott
    Apr 13, 2011 at 13:52
0

Hi I have been researching this for a while, came up with the following hack. In wp-admin/new_post.php add the following, after the wp_die line:

wp_die( __('Invalid post type') );

if ( !current_user_can( 'manage_options' ) and $post_type == 'page') {
    echo "To create new pages please ask site admin";
    exit;
} //if user is not admin, a warning is issued and script terminated

then in wp-admin/menu, add a conditional for showing the add page submenu in admin. The line is normally there but not included within the if statement

if ( current_user_can( 'manage_options' )) { //if user is admin, show submenu for creating new pages, else no luck
   $submenu['edit.php?post_type=page'][10] = array( _x('Add New', 'page'), get_post_type_object( 'page' )->cap->create_posts, 'post-new.php?post_type=page' );
}

I still have to figure out how to remove the add new button in the edit pages, however users with publish rights can now edit existing pages but not create new pages. If they press the new page button they get a warning and cannot proceed further. Posts are not affected. The same code could easily be applied to prevent the creation of new posts aswell with just little editing.

Hope this helps - Ackab

6
  • This is wrong. It will break after the next WordPress update.
    – fuxia
    Dec 23, 2012 at 0:31
  • toscho will the next release include a create posts capability? Anyway this works for what I need, in the current version. Do you have a better/cleaner way to do it in the current version?
    – Ackab
    Dec 23, 2012 at 0:34
  • If you read the ticket @scribu linked to, you will see this capability is already in WordPress 3.5.
    – fuxia
    Dec 23, 2012 at 0:38
  • toscho I am using role scoper, and cannot see a way to prevent a user from creating new pages. If I assign publish rights, then he can create new pages. Do you have an hint as to how I block a role from creating new pages with the now available capability. Just a little pointer would be great thanks
    – Ackab
    Dec 23, 2012 at 0:45
  • remove_cap( $role, $cap )
    – fuxia
    Dec 23, 2012 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.