0

I'm trying to remove the capability of users other than the administrator/superadmin (ID number 1) to add a page, I know that there are plugins which you can edit the wordpress role but in my case I need it to be per user/username/userid (no plugin that I no of is available)..

Currently user number 2 needs to be 'Administrator' because a specific plugin I use only displays reports to 'Administrator' role but I need to remove add page capabilities. I have the following code:

function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
  $userdata->ID != 1 ->remove_cap('publish_pages');

 }

add_action('admin_init','modify_capabilities');

But it doesn't work.. The error is in this line:

$userdata->ID != 1 ->remove_cap('publish_pages');

1
  • 1
    And what does $userdata->ID != 1 ->remove_cap('publish_pages'); line supposed to do? Which object's remove_cap() method you're invoking?
    – zerkms
    Sep 14, 2011 at 1:02

1 Answer 1

0

Your code seems off by a bit:

function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
    if ($userdata->ID != 1) {
         $role = get_role('author');
         $role->remove_cap('publish_pages');
         $role->remove_cap('publish_posts');
    }
 }

add_action('admin_init','modify_capabilities');

Updated based on your comments and taking the original details from the blog you linked to. Not sure why you removed those parts though...

6
  • this is the original code (I just modified it): $editor_role = get_role('editor'); $editor_role->remove_cap('publish_pages'); from here: erisds.co.uk/wordpress/…
    – Laurize
    Sep 14, 2011 at 3:47
  • I also get this error when i tried your code: Fatal error: Call to a member function remove_cap() on a non-object on this line: $somevariable->remove_cap('publish_pages');
    – Laurize
    Sep 14, 2011 at 3:52
  • Like I said, you need to change $somevariable to be the correct variable. From your other comment it looks like it needs to be $editor_role but you'll need to make sure that you have initialised the $editor_role variable. Sep 14, 2011 at 7:46
  • hmm.. the reason why I didn't use $editor_role because it's not the editor role that I want to remove the capability.. I wanted to remove the capability to users that don't have 1 as their user ID.. I hope I explained it well...
    – Laurize
    Sep 14, 2011 at 8:34
  • That's why I changed it to author (see my edit). This obviously will only work for people creating pages/posts. That post you linked to also spoke about removing Menu items per user, which perhaps would be a better way? Sep 14, 2011 at 9:37

Your Answer

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

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