There are many great code snippets in their Gitlab. You might find more on their…
Allow users to only edit some pages and their children
This code checks for the parent and allows a logged-in user to edit this pages and all descendants of that page. Just edit two parts: The user ID and the parent Page ID and you are good to go.
function check_allow_edit ( $caps, $cap, $user_id, $args ) {
$to_filter = [ 'edit_post', 'delete_post', 'edit_page', 'delete_page' ];
// If the capability being filtered isn't of our interest, just return current value
if ( ! in_array( $cap, $to_filter, true ) ) {
return $caps;
}
// First item in $args array should be page ID
$page_id = $args[0];
$page = get_post( $page_id );
/*
if ( ! $current_page_id ) {
//cannot edit
$can_edit = false;
}
*/
if ($page->post_parent) {
$ancestors = get_post_ancestors($page->ID);
$root = count($ancestors)-1;
$page_id = $ancestors[$root];
}
if ($user_id == 137) { //change to the user ID you want
$allowed_ids = array('37'); //allow this page and children of it
if (in_array($page_id, $allowed_ids)) {
//user 1 and current page is allowed - can edit
$can_edit = true;
} else {
//user 1 and current page is not allowed - cannot edit
$can_edit = false;
}
} else {
//not the specified user so edit all
$can_edit = true;
}
// First item in $args array should be page ID
//if ( ! $args || empty( $args[0] ) || ! wpse_user_can_edit( $user_id, $args[0] ) ) {
if ( ! $args || empty( $args[0] ) || $can_edit == false ) {
// User is not allowed, let's tell that to WP
return [ 'do_not_allow' ];
}
// Otherwise just return current value
return $caps;
};
add_filter( 'map_meta_cap', 'check_allow_edit', 10, 4 );
Dieser Beitrag hat 0 Kommentare