7

As the title says, I'd like to restrict back-end access to certain pages for certain users.

While doing a site with 45-50 pages, I realised it would be a much better user experience if the Page menu only listed those pages which the user should be able to change/update.

I've tried the plugins below to no avail. Advanced access manager has the functionality but does not work/is buggy on 3.5.1.

http://wordpress.org/extend/plugins/advanced-access-manager http://wordpress.org/extend/plugins/role-scoper http://wordpress.org/extend/plugins/adminimize http://wordpress.org/extend/plugins/s2member

Code snippet in functions.php?

3
  • 1
    You want to control access on a per-user basis? That is, you can't use Roles or Capabilities to do it?
    – s_ha_dum
    Mar 18, 2013 at 14:27
  • Access/view based on Roles or Capabilities will work!
    – BoBoz
    Mar 18, 2013 at 14:31
  • I should emphasize that the importent part is for the specified user/role not to see certain pages in back-end. As opposed to not be able to edit certain pages.
    – BoBoz
    Mar 18, 2013 at 14:36

2 Answers 2

7

This code seems to work well for me (in functions.php):

$user_id = get_current_user_id();
if ($user_id == 2) {
    add_filter( 'parse_query', 'exclude_pages_from_admin' );
}

function exclude_pages_from_admin($query) {
    global $pagenow,$post_type;
    if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
        $query->query_vars['post__not_in'] = array('123','234','345');
    }
}
7
  • This is quite the nice answer, +1. Note that you can accept your own answers as correct... Sep 18, 2014 at 3:10
  • Ok, this will be done :)
    – BoBoz
    Sep 18, 2014 at 17:51
  • BoBoz's answer worked great for a while, now I'm getting a Warning: "call_user_func_array() expects parameter 1 to be a valid callback, function 'exclude_pages_from_admin' not found or invalid function name". What would be the best way to fix this code snippet so that it doesn't produce the warning? Thanks! Jan 13, 2016 at 19:09
  • I am new in Wordpress. let me know where is function.php is located. is it is in wp-includes folder. if so I get the error Call to undefined function add_filter().
    – Nishad Up
    Feb 18, 2016 at 9:35
  • Right now, my user is an "Author" and doesn't have "Pages" menu visible in his admin section. I don't want to increase his permissions any more than what's needed to give him access to three pages. I have put the code in but the user has no way to access the pages because they don't have "Pages" in the left menu. How would I go about showing that menu item so when he clicks on it, he'll see his three pages that he's permitted to edit? If nothing else, if I could show the "Edit" link on the page itself, that would work, too. But this code snippet doesn't provide that functionality, either. Feb 25, 2017 at 21:48
4

It won't let me comment, so I'm adding this as a new answer.

The error:

Warning: "call_user_func_array() expects parameter 1 to be a valid callback, >function 'exclude_pages_from_admin' not found or invalid function name".

... is due to the function being called only after the user_id is checked. So if you're logged in NOT as that user, the function doesn't exist, and the filter returns that error, since it is looking for the function, but can't find it.

So it should be:

add_filter( 'parse_query', 'exclude_pages_from_admin' );

function exclude_pages_from_admin($query) {
    $user_id = get_current_user_id();

    if ($user_id == 2) {
    global $pagenow,$post_type;
     if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
        $query->query_vars['post__not_in'] = array('123','234','345');
     }
    }
}
1
  • I edited the original answer to fix the wrong callback registration.
    – Tim
    Jun 9, 2020 at 13:45

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.