Site icon Hip-Hop Website Design and Development

take away "Edit" from choose pages

I’ve had a very good go searching earlier than asking this query so if it is already been cracked simply level me in that course!

I’ve arrange some customized core pages with the intention to work as "archive" pages for customized taxonomies. I did this by creating static pages then referred to as a operate to exchange the template for every web page with a customized template. Every template queries and shows all phrases within the related taxonomy. This half works nice.

Now I am attempting to take away enhancing capabilities from these pages within the WP Dashboard for ALL consumer roles. I choose to not use a plugin + I’ve but to see one which solves my specific drawback.

I’ve discovered a script that works to cover the pages from the pages space and it really works nicely:

operate hide_pages_from_edit( $question ) {
    if( !is_admin() )
        return $question;

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

    return $question;
};

add_action( 'pre_get_posts', 'hide_pages_from_edit' );

Nevertheless, if an admin is logged in and navigates to certainly one of these pages, the "Edit Page" hyperlink remains to be within the admin bar. Clearly the code above doesn’t goal this, but it surely made me wish to discover a higher resolution.

I discovered this resolution to the same query however couldn’t get it working. Even the offered code simply removes enhancing capabilities from all pages/posts. Right here is the code from that reply:

operate wpse_user_can_edit( $user_id, $page_id ) {

   $web page = get_post( $page_id );

   // let's discover the topmost web page within the hierarchy
   whereas( $web page && (int) $page->father or mother ) {
     $web page = get_post( $page->father or mother );
   }

   if ( ! $web page ) {
     return false;
   }

   // now $web page is the highest web page within the hierarchy
   // the way to know if an consumer can edit it, it is as much as you...

}

add_filter( 'map_meta_cap', operate ( $caps, $cap, $user_id, $args ) {

    $to_filter = [ 'edit_post', 'delete_post', 'edit_page', 'delete_page' ];

    // If the potential being filtered is not of our curiosity, simply return present worth
    if ( ! in_array( $cap, $to_filter, true ) ) {
        return $caps;
    }

    // First merchandise in $args array needs to be web page ID
    if ( ! $args || empty( $args[0] ) || ! wpse_user_can_edit( $user_id, $args[0] ) ) {
        // Person is just not allowed, let's inform that to WP
        return [ 'do_not_allow' ];
    }
    // In any other case simply return present worth
    return $caps;

}, 10, 4 );

For instance I wished to take away the edit capabilities from pages by title is_page( 'artist' ) or by id get_post( 411 ) – I’ve tried each strategies like this:

operate wpse_user_can_edit( $user_id ) {

   $web page = get_post( 411 );

   if ( ! $web page ) {
     return false;
   }

};

Be aware: I am not together with the add_filter() half from above right here as a result of I didn’t change it.

Nothing appears to work, it nonetheless removes the power to edit/delete from all pages/posts.