Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Restrict editing pages by user role?
- This topic has 22 replies, 6 voices, and was last updated 9 years, 1 month ago by Vladimir.
-
AuthorPosts
-
09/09/2014 at 18:48 #906StevenJGoodParticipant
I have seen that I am able to restrict users from editing pages by entering the ID when editing a specific user, but I need to restrict page editing access by user role.
Is it possible to restrict editing of specific posts by user role?
10/09/2014 at 01:47 #907VladimirKeymaster1) Select needed role.
2) Input ‘pages’ to the ‘Quick Filter’ field. All user capabilities related to the ‘pages’ will be selected.
3) Unselect them all.
4) Update role.
Users with this role will not have access to adding/editing/deleting any pages.10/09/2014 at 14:47 #911StevenJGoodParticipantSorry, I think I was unclear. I know that I am able to restrict access to adding/editing/deleting all pages. I am trying to restrict the ability to edit/delete specific pages by user role.
Example: Editor role can add/edit/delete pages but cannot edit/delete “About” and “Join” pages.
Is this possible?
11/09/2014 at 02:17 #958VladimirKeymasterSorry, that was my fault. I did not pay attention on the word ‘specific’ at your 1st message.
Current version does not include the interface to restrict editing posts/pages by role. It is good feature request and I will add this functionality to Pro version with time.
Now you may use this piece of code to prohibit your users with ‘editor’ role access to the list of pages:if (is_blog_admin() && current_user_can('editor')) { add_action('pre_get_posts', 'restrict_posts_list'); function restrict_posts_list($query) { $suppressing_filters = $query->get('suppress_filters'); // Filter suppression on? if ($suppressing_filters) { return query; } if ($query->query['post_type']=='page') { $posts_restriction_type = 2; // Prohibit $posts_list = array(442, 484); // comma separated list of pages IDs if ($posts_restriction_type==1) { // Allow $query->set('post__in', $posts_list); } else { // Prohibit $query->set('post__not_in', $posts_list); } } return $query; } // restrict_posts_list() }
Replace pages ID at $posts_list array and add this code to your active theme functions.php file.
23/10/2014 at 17:50 #1246JJSpringerSpectatorThis ability would be useful for me, as well. I need to have certain users be able to only edit certain pages.
24/10/2014 at 00:40 #1247VladimirKeymaster@JJSpringer: Thanks for letting me know. This feature will be available until the end of this year.
15/07/2015 at 12:51 #1584oopgoParticipantIs this feature available now? We are looking same type of functionality. If this is not available then can you please let us know where we need to add your code which you have provided in this thread.
15/07/2015 at 14:08 #1585VladimirKeymasterIt is possible to set posts/pages edit restrictions for selected users:
https://www.role-editor.com/allow-user-edit-selected-posts/
Plugin still does not support such restrictions for the roles.You may insert the provided code into your active theme functions.php file.
13/09/2015 at 23:34 #1697AntKatSpectatorHi Vladimir, I too am looking for the restrict pages by user role. +1 for this feature.
14/09/2015 at 05:19 #1698VladimirKeymasterHi,
Thanks for letting me know. I will add it.
22/09/2015 at 00:51 #1709AntKatSpectatorVladimir,
How might this work on a multisite with replicated content? We provide template websites where all sites have a set of pages (we use mirroring so that we can globally change these specific pages). We do not want anyone except super-admin to edit these pages but once created, individual sites can add more pages. So I can see that the code above would initially work across the network because the page IDs of the original blog template will be the same.However, wouldn’t this be a problem in the future if we ever wanted to add new mirrored pages across the network because the page IDs for all of the sites will not be the same if some sites add their own pages.
Could the code be written to use slugs rather than IDs?
23/09/2015 at 03:28 #1711VladimirKeymasterAntKat,
This is a version of code modified to work with a list of pages slugs:add_action('pre_get_posts', 'restrict_posts_list'); function restrict_posts_list($query) { if ( !(is_blog_admin() && current_user_can('editor')) ) { return $query; } $suppressing_filters = $query->get('suppress_filters'); // Filter suppression on? if ($suppressing_filters) { return query; } if ($query->query['post_type']=='page') { $posts_restriction_type = 2; // Prohibit $posts_slugs = array( 'jobs', 'post-to-our-blog', 'sample-page' ); $posts_list = array(); // comma separated list of pages IDs foreach($posts_slugs as $post_slug) { $page = get_page_by_path($post_slug); if ($page) { $posts_list[] = $page->ID; } } if (empty($posts_list)) { return $query; } if ($posts_restriction_type==1) { // Allow $query->set('post__in', $posts_list); } else { // Prohibit $query->set('post__not_in', $posts_list); } } return $query; } // restrict_posts_list()
29/09/2015 at 16:51 #1713[email protected]ParticipantVladimir,
In a Multisiste environment, can I use this code to restrict editing pages by user role that was created by Role Editor Pro, NOT one of the WordPress built-in roles
29/09/2015 at 17:15 #1715[email protected]ParticipantThis code currently blocks pages 3,4,5 fro all roles NOT just the “franchise” role how I allow the administrator and SUPER administrator to see/edit this page also?
Thank you so much!
if (is_blog_admin() && current_user_can(‘franchise’)) {
add_action(‘pre_get_posts’, ‘restrict_posts_list’);
function restrict_posts_list($query) {$suppressing_filters = $query->get(‘suppress_filters’); // Filter suppression on?
if ($suppressing_filters) {
return query;
}if ($query->query[‘post_type’]==’page’) {
$posts_restriction_type = 2; // Prohibit
$posts_list = array(3,4,5); // comma separated list of pages IDs
if ($posts_restriction_type==1) { // Allow
$query->set(‘post__in’, $posts_list);
} else { // Prohibit
$query->set(‘post__not_in’, $posts_list);
}
}return $query;
}
// restrict_posts_list()
}29/09/2015 at 17:25 #1716VladimirKeymasterAdd to the begin of function:
function restrict_post_list($query) { if (is_super_admin() || current_user_can('administrator')) { return $query; } ...
or change the initial
if
condition to:if (is_blog_admin() && !(is_super_admin() || current_user_can('administrator')) && current_user_can('franchise')) {
-
AuthorPosts
- You must be logged in to reply to this topic.