0

I want to hide / remove a particular menu item from +New menu on admin menu bar based on specific user role. I referred to this post and tried what is suggested here but for me it did not work.

I have a user role fuel-surcharge-editor, which should not get access to Weather Alert content type (please see screenshot).

enter image description here

Both Fuel Surcharge and Weather Alert are custom post types. So from +New menu both are pointing to same location:

http://vagrant.local/wp/wp-admin/post-new.php?post_type=fuel-surcharge, with a different post_type.

I tried the following:

function remove_new_content_items(){
    if(current_user_can('fuel-surcharge-editor')) {
       global $wp_admin_bar;
       $wp_admin_bar->remove_menu( 'post-new', 'weather_alert' );
    }
}

add_action( 'wp_before_admin_bar_render', 'remove_new_content_items', 0 );

I also tried $wp_admin_bar->remove_menu('post-new.php?post_type=weather_alert'); and $wp_admin_bar->remove_menu('post-new.php', 'weather_alert');

but none worked. How can I do this?

4
  • has user role fuel-surcharge-editor the capabilitie to create this CPT ?
    – mmm
    Commented Jul 13, 2017 at 11:24
  • Thank you for your reply. If CPT = Post type content, then yes. Actually I got the answer. It has to be remove_node, not remove_menu. Reference: codex.wordpress.org/Function_Reference/remove_node. Commented Jul 13, 2017 at 11:35
  • instead of removing the item in the "New" menu, it can be more intersting to remove the create capabilitie. if you just modify the menu, user can always go to the URL to create a "weather_alert"
    – mmm
    Commented Jul 13, 2017 at 15:33
  • So you mean remove_menu or remove_node does not actually blocks the accessibility permanently? Can you please suggest me some articles to read on about handling capabilities and how it is used to "remove menu"? Commented Jul 13, 2017 at 16:06

0

Your Answer

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

Browse other questions tagged or ask your own question.