1

I am trying to remove some menu-links capabilities from the wp-admin for a user role called wsm. I have tried many ways and when i just add the code below it will remove those links from the menu for all user roles.

The problem is that it also strips the menus from my own admin as Super Admin. So basically it removes the menu links from any role.

I tried to add this line: { if (current_user_can('administrator')) but it didn't change anything. Maybe i added it in the wrong place?

I need to remove links also based on certain post types as you can see below.

add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions_testimonials( $actions )
{ 
    if( get_post_type() === 'wiki-testimonials' )
        unset( $actions['view'] );
        unset( $actions['edit'] );
    return $actions;
}
add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions_staff( $actions )
  {
    if( get_post_type() === 'wiki-staff' )
        unset( $actions['view'] );
        unset( $actions['delete'] );
        unset( $actions['trash'] );
        unset( $actions['edit'] );
    return $actions;
}

If someone can help me to produce the correct solution I would be very greatfull. Thanks in advance.

1 Answer 1

0

You can add a condition on capabilities in order to exclude the admin from the unset action for example

add_filter( 'post_row_actions', 'remove_row_actions', 10, 2 );
function remove_row_actions_staff( $actions, $post ){
  //This line stops function execution if user has some admin capabilities 
  if( current_user_can( 'administrator' ) ):
    return $actions;
   endif; 

  if( $post->post_type() === 'wiki-testimonials' )
    unset( $actions['view'] );
    unset( $actions['edit'] );
  return $actions;
}
7
  • Hi Cravon, Thanks for your reply. I have tried your code, but this also removes my menu-links as administrator? M. Commented Jan 11, 2022 at 11:46
  • Hi Marc, you right : give a try to my edited version : the problem was a void return. I mixed up filter and action sorry.
    – CRavon
    Commented Jan 11, 2022 at 12:10
  • Hi, Tried that, but it didn't work. I made a small alteration and changed "edit-user" for "administrator" That made it work to hide menu-links from my wsm user and keep all capabilities for administrator. It just does not apply to a specific post type, but for all the post types. add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 ); function remove_row_actions( $actions ){ if( current_user_can( 'administrator' ) ): return $actions; endif; if( get_post_type() === 'wiki-testimonials' ){ unset( $actions['view'] ); return $actions; }} Commented Jan 11, 2022 at 13:07
  • Hi did you try to get post type with the $post object (second parameter on the filter) ? I show this on my edit.
    – CRavon
    Commented Jan 11, 2022 at 13:22
  • Hi CRavon, Unfortunately it produces a critical error. Thnx. Marc Commented Jan 11, 2022 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.