Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Protecting specific page from being edited
- This topic has 14 replies, 2 voices, and was last updated 7 years, 2 months ago by Vladimir.
-
AuthorPosts
-
10/10/2017 at 21:16 #4265chingalingParticipant
Hi
I want to protect certain pages from being edited by anyone other than the Administrator.
I found one approach here: http://www.wpbeginner.com/plugins/how-to-allow-editors-to-only-edit-certain-pages-in-wordpress/
But I’d much prefer to use URE. Is there a feature or way I can do this with URE?
The pages are mainly those which have vital shortcodes in them, hence why I do not want users to be able to edit/delete them – whilst still retaining access to edit other pages.11/10/2017 at 02:27 #4267VladimirKeymasterHi,
You can achieve this with Edit access restrictions add-on. You can prohibit with it the editing of a list of pages by ID for the editor role.
11/10/2017 at 05:13 #4269chingalingParticipantHi Vladimir
Thank you for the link. I got as far as adding a new role, but I can’t seem to find how to access this panel:
Also, looking at the way the plugin works, I’m not sure how I would do it because I’m using it on a Multisite where a user can sign up for a website (which would take its content from one of my subsites as a template) and upon signup they are given the EDITOR role. In the template which their website is based on, there are a few PAGES (ecommerce and account related) which need to be protected from anyone other than ADMIN/NETWORK ADMIN. Is this achievable please?
11/10/2017 at 06:18 #4272VladimirKeymasterThis screenshot belongs to other add-on: Content view restrictions, which works for front-end.
I recommended to use another one. If you provide that those restricted pages will have the same IDs at any subsite of your network, you can setup the list of restricted pages, like “1, 3, 7” via custom filters ‘ure_edit_posts_access_id_list’ and ‘ure_edit_posts_access_restriction_type’ as a must use plugin – which will work for any subsite.
add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list'); function ure_edit_posts_access_set_id_list($list) { $current_user = wp_get_current_user(); $lib = URE_Lib_Pro::get_instance(); if ($lib->user_can_role($current_user, 'editor')) { $list .= ',1,3,7'; } return $list; } add_filter('ure_edit_posts_access_restriction_type', 'ure_edit_posts_access_set_restriction_type'); function ure_edit_posts_access_set_restriction_type($restriction_type) { $current_user = wp_get_current_user(); $lib = URE_Lib_Pro::get_instance(); if ($lib->user_can_role($current_user, 'editor')) { $restriction_type = 2; // Block } return $restriction_type; }
11/10/2017 at 06:28 #4273chingalingParticipantThank you, I will try test that later today.
One thing I can foresee being a potential issue, is that the pages aren’t guaranteed to be the same.
For example the page ID for the Checkout page (with a permalink of checkout) for site 1 is likely to be different to the page ID for the Checkout page on site 2, 3 and 4.Is it possible to restrict those pages based on permalink?
11/10/2017 at 06:38 #4276VladimirKeymasterIt possible to extend a logic of the ‘set_id_list’ function using WP function url_to_postid(), in order to return a page ID related to current site.
11/10/2017 at 14:16 #4278chingalingParticipantHi Vladimir
That sounds a bit beyond my technical understanding at the moment, is that something you can help with the code for please?
11/10/2017 at 14:36 #4279VladimirKeymasterGive me more details. Does it one page for a subsite?
Show a list of pages with ID and permalinks (without domain) and describe conditions, how will you decide what permalink for what site to select.11/10/2017 at 15:04 #4280chingalingParticipantHi
I hope this helps explain it better:
So for example, in my multisite network I couldnt lock the pages by ID, because depending on when the pages are created, it may not be the ID for the page I want to lock, however I could make sure that certain permalinks are reserved for the multisite for the locked pages hence why I’d like to be able to define the (reserved) permalinks for which are to be locked.
I hope that makes sense 🙂
12/10/2017 at 05:32 #4281VladimirKeymasterThis code does not depend from a page ID. It blocks edit access for pages with ‘shop’ and ‘busket’ slugs for any user with ‘edit_others_pages’ capability for any subsite.
add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list'); function ure_edit_posts_access_set_id_list($list) { $current_user = wp_get_current_user(); $lib = URE_Lib_Pro::get_instance(); if (!current_user_can('edit_others_pages')) { return $list; } $slugs = array('shop', 'busket'); $id_list = array(); foreach($slugs as $slug) { $page = get_page_by_path($slug); if (!empty($page)) { $id_list[] = $page->ID; } } $id_list_str = implode(',', $id_list); $list .= $id_list_str; return $list; } add_filter('ure_edit_posts_access_restriction_type', 'ure_edit_posts_access_set_restriction_type'); function ure_edit_posts_access_set_restriction_type($restriction_type) { $current_user = wp_get_current_user(); $lib = URE_Lib_Pro::get_instance(); if (current_user_can('edit_others_pages')) { $restriction_type = 2; // Block } return $restriction_type; }
If you will wish to add some edit restriction for user with ‘edit_others_pages’ manually, you should take into account that this restricton should be the same type “Block” in order to not break this code and vice versa.
13/10/2017 at 22:16 #4284chingalingParticipantThank you Vladimir
I added that code in as a Must Use plugin but the Editor can still seem to edit the shop (and basket – assuming its a typo) pages.
To note, I did also try the first approach, with the ID numbers and that did not work either for me
15/10/2017 at 03:46 #4285VladimirKeymasterLook how it works at my test site – demo video.
Slightly enhanced code version. Update your copy:
add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list'); function ure_edit_posts_access_set_id_list($list) { if (!current_user_can('edit_others_pages')) { return $list; } $slugs = array('shop', 'busket'); // input blocked pages slug here $id_list = array(); foreach($slugs as $slug) { $page = get_page_by_path($slug); if (!empty($page)) { $id_list[] = $page->ID; } } if (count($id_list)>0) { $id_list_str = implode(',', $id_list); $list .= $id_list_str; } return $list; } add_filter('ure_edit_posts_access_restriction_type', 'ure_edit_posts_access_set_restriction_type'); function ure_edit_posts_access_set_restriction_type($restriction_type) { if (current_user_can('edit_others_pages')) { $restriction_type = 2; // Block } return $restriction_type; }
15/10/2017 at 04:11 #4286chingalingParticipantThats fantastic – it works now! You’re amazing.
I didnt have the Content Restrictions boxes ticked (was working off a backup)
15/10/2017 at 04:14 #4287chingalingParticipantA bit off-topic but: it looks like you have a local development environment for a Multisite. I’ve been struggling to set up a Multisite local development environment on my pc; do you have any links to resources which guide through how to do it at all please?
Thanks
15/10/2017 at 05:42 #4288VladimirKeymasterI followed this article always.
And selected “Subdirectories — a path-based network in which on-demand sites use paths” to not struggle configuring Apache with subdomains. -
AuthorPosts
- You must be logged in to reply to this topic.