Opened 3 weeks ago
Last modified 3 weeks ago
#62074 new feature request
Hooks to remove Add New links
Reported by: | markuscode | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Administration | Keywords: | |
Focuses: | Cc: |
Description
A came across actions that been used for menus and realise that "+ New" content area at the top bar in admin panel are cannot be replaced or removed.
For example I need to hide or replace ability to add new custom post type.
Now there is now way to do that.
Suggesting add some filter to the loop
File: "wp-includes/admin-bar.php"
Function "wp_admin_bar_new_content_menu"
Before:
<?php foreach ( $cpts as $cpt ) { if ( ! current_user_can( $cpt->cap->create_posts ) ) { continue; } $key = 'post-new.php?post_type=' . $cpt->name; $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name ); }
After:
<?php foreach ( $cpts as $cpt ) { if ( ! current_user_can( $cpt->cap->create_posts ) ) { continue; } $key = 'post-new.php?post_type=' . $cpt->name; if ( ! apply_filters( "show_new_content_{$cpt->name}_in_admin_bar",true, $key, $cpt ) ) { continue; } $actions[ $key ] = array( $cpt->labels->name_admin_bar, 'new-' . $cpt->name ); }
Usage Example:
<?php add_filter( "show_new_content_my_post_type_in_admin_bar", "__return_false", 999 );
Change History (3)
#3
@
3 weeks ago
- Component changed from Toolbar to Administration
- Summary changed from wp_admin_bar_new_content_menu issue to Hooks to remove Add New links
The toolbar link can be removed already by setting the custom post type's show_in_admin_bar
parameter to false
(either in register_post_type or register_{$post_type}_post_type_args).
To edit the toolbar link, you could use the wp_before_admin_bar_render
hook.
<?php function replace_new_cpt_in_admin_bar() { global $wp_admin_bar; $node = $wp_admin_bar->get_node( 'new-cpt' ); // Return early if node contents are not available to edit. if ( ! isset( $node->title ) ) { return; } $wp_admin_bar->add_node( array( 'id' => 'new-cpt', 'title' => 'New Custom Post Type link text', ) ); } add_action( 'wp_before_admin_bar_render', 'replace_new_cpt_in_admin_bar' );
Removing a .page-title-action
link seems more complicated. If hiding the link is good enough, you could add custom admin CSS with admin_enqueue_scripts
(or with a plugin such as Simple Custom CSS and JS):
.post-type-cpt .page-title-action { display: none; }
Same thing with button in title on the table list;
File: "wp-admin/edit-form-advanced.php"
Before:
After:
And:
File: "wp-admin/edit.php"
Before:
After: