admin – remove “Edit” from select pages

Question

I’ve had a good look around before asking this question so if it’s already been cracked just point me in that direction!

I’ve set up some custom core pages in order to work as “archive” pages for custom taxonomies. I did this by creating static pages then called a function to replace the template for each page with a custom template. Each template queries and displays all terms in the associated taxonomy. This part works great.

Now I’m trying to remove editing capabilities from these pages in the WP Dashboard for ALL user roles. I prefer not to use a plugin + I’ve yet to see one that solves my particular problem.

I’ve found a script that works to hide the pages from the pages area and it works well:

function hide_pages_from_edit( $query ) {
    if( !is_admin() )
        return $query;

    global $pagenow;
    if( 'edit.php' == $pagenow && 'page' == get_query_var( 'post_type' ) )
        $query->set( 'post__not_in', array( 411, 414, 419, 421 ) ); // <- page ID to hide

    return $query;
};

add_action( 'pre_get_posts', 'hide_pages_from_edit' );

However, if an admin is logged in and navigates to one of these pages, the “Edit Page” link is still in the admin bar. Obviously the code above does not target this, but it made me want to find a better solution.

I found this solution to a similar question but could not get it working. Even the provided code just removes editing capabilities from all pages/posts. Here is the code from that answer:

function wpse_user_can_edit( $user_id, $page_id ) {

   $page = get_post( $page_id );

   // let's find the topmost page in the hierarchy
   while( $page && (int) $page->parent ) {
     $page = get_post( $page->parent );
   }

   if ( ! $page ) {
     return false;
   }

   // now $page is the top page in the hierarchy
   // how to know if an user can edit it, it's up to you...

}

add_filter( 'map_meta_cap', function ( $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
    if ( ! $args || empty( $args[0] ) || ! wpse_user_can_edit( $user_id, $args[0] ) ) {
        // User is not allowed, let's tell that to WP
        return [ 'do_not_allow' ];
    }
    // Otherwise just return current value
    return $caps;

}, 10, 4 );

Let’s say I wanted to remove the edit capabilities from pages by title is_page( 'artist' ) or by id get_post( 411 ) – I’ve tried both methods like this:

function wpse_user_can_edit( $user_id ) {

   $page = get_post( 411 );

   if ( ! $page ) {
     return false;
   }

};

Note: I’m not including the add_filter() part from above here because I did not change it.

Nothing seems to work, it still removes the ability to edit/delete from all pages/posts.

0
Shelaine 1 month 2021-10-21T20:25:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse