I've searched all day for this and couldn't find a plugin that allow me to restrict the access to specific pages in the wp-admin backed by user role.
Example: I have several users on my website and I want them to only see the pages that they are allowed to edit. I've done this manually by code and it's working (see code below), but I wanted a plugin that allowed me to the same thing.
Why? Because I have a support team that manages the site and they can't code. Every time I need to add a page or allow someone else on some page I need to manually go there and edit that file.
The closest I came to a solution was the Pages by User Role plugin, on CodeCanyon, but it doesn't work on the backend. Something similar that works on the backend would be great.
if(current_user_can('custom-editor')){
function allowed_pages_custom_e($query) {
global $pagenow,$post_type;
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__in'] = array('11678');
}
}
add_filter( 'parse_query', 'allowed_pages_custom_e' );
}
I've searched all day for this and couldn't find a plugin that allow me to restrict the access to specific pages in the wp-admin backed by user role.
Example: I have several users on my website and I want them to only see the pages that they are allowed to edit. I've done this manually by code and it's working (see code below), but I wanted a plugin that allowed me to the same thing.
Why? Because I have a support team that manages the site and they can't code. Every time I need to add a page or allow someone else on some page I need to manually go there and edit that file.
The closest I came to a solution was the Pages by User Role plugin, on CodeCanyon, but it doesn't work on the backend. Something similar that works on the backend would be great.
if(current_user_can('custom-editor')){
function allowed_pages_custom_e($query) {
global $pagenow,$post_type;
if (is_admin() && $pagenow=='edit.php' && $post_type =='page') {
$query->query_vars['post__in'] = array('11678');
}
}
add_filter( 'parse_query', 'allowed_pages_custom_e' );
}
Share
Improve this question
edited Oct 16, 2016 at 1:09
Dave Romsey
17.9k11 gold badges56 silver badges70 bronze badges
asked Oct 16, 2016 at 0:43
bfagundesbfagundes
131 silver badge4 bronze badges
3
- Ultimate member might be a better choice to go with – user104130 Commented Oct 16, 2016 at 1:46
- I'd prefer if it wouldn't be a plugin. – bfagundes Commented Oct 21, 2016 at 13:15
- My answer was UM because you have mentioned that you couldn't find a plugin that allow to restrict pages by User Role. Check the answer section, i have written some code for you. – user104130 Commented Oct 26, 2016 at 9:51
2 Answers
Reset to default 2if( is_admin() ) {
add_filter( 'init', 'custom_restrict_pages_from_admin' );
}
function custom_restrict_pages_from_admin() {
global $pagenow;
$arr = array(
'update-core.php',
'edit.php'
);
if( custom_check_user_roles() && in_array( $pagenow , $arr ) ) {
//echo some custom messages or call the functions
} else {
echo 'This page has restricted access to specific roles';
wp_die();
}
}
function custom_check_user_roles() {
//restrict pages by user role
if( current_user_can( 'administrator' ) || current_user_can( 'editor' ) )
return true;
else {
echo 'Not allowed!';
wp_die();
}
}
Use Advanced Access Manager ( https://wordpress/plugins/advanced-access-manager/ ) Plugin to restrict users in backend according to their roles....
I thing this will be a better solution for your issue.