Make WordPress Core

Opened 3 weeks ago

Last modified 3 weeks ago

#62074 new feature request

Hooks to remove Add New links

Reported by: markuscode's profile 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)

#1 @sabernhardt
3 weeks ago

  • Component changed from Administration to Toolbar

#2 @markuscode
3 weeks ago

Same thing with button in title on the table list;

File: "wp-admin/edit-form-advanced.php"
Before:

<?php
if ( isset( $post_new_file ) && current_user_can( $post_type_object->cap->create_posts ) ) {
        echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
}

After:

<?php
<?php

if ( isset( $post_new_file )
&& current_user_can( $post_type_object->cap->create_posts )
&& apply_filters( "show_new_action_{$post_type_object->name}_in_admin",true, $post_type_object )
) {
        echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
}
?>

And:
File: "wp-admin/edit.php"
Before:

<?php
if ( current_user_can( $post_type_object->cap->create_posts ) ) {
        echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
}

After:

<?php
<?php
if (
current_user_can( $post_type_object->cap->create_posts )
&& apply_filters( "show_new_action_{$post_type_object->name}_in_admin",true, $post_type_object )
) {
        echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action">' . esc_html( $post_type_object->labels->add_new ) . '</a>';
}
?>
Last edited 3 weeks ago by markuscode (previous) (diff)

#3 @sabernhardt
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;
}
Note: See TracTickets for help on using tickets.