最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Change order of an item under "New" on the admin bar

programmeradmin0浏览0评论

The Admin Bar has a menu with shortcuts for adding new content.

By default, it shows "Post", "Media", "Page" and "User".

I'm using register_post_type to register a new custom post type. I can use its menu_position property to change the position in the dashboard menu, but how can I reorder the shortcut link so it will show higher, instead of just above "User"?

The Admin Bar has a menu with shortcuts for adding new content.

By default, it shows "Post", "Media", "Page" and "User".

I'm using register_post_type to register a new custom post type. I can use its menu_position property to change the position in the dashboard menu, but how can I reorder the shortcut link so it will show higher, instead of just above "User"?

Share Improve this question asked Jul 13, 2017 at 20:44 That Brazilian GuyThat Brazilian Guy 1,2413 gold badges19 silver badges43 bronze badges 1
  • 1 Possibly related: wordpress.stackexchange/q/76642/22510 – That Brazilian Guy Commented Jul 14, 2017 at 14:26
Add a comment  | 

2 Answers 2

Reset to default 4

I had a similar situation. My custom post type showed up under the Admin Bar as Artifact, but my clients preferred it to be at the top of the list. Plus, the items for Media and User were not really needed.

My approach was first to user remove_node to take away all the ones except my custom post-type and then put back the ones I want with add_node

add_action( 'wp_before_admin_bar_render', 'portfolio_adminbar' );

function portfolio_adminbar() {

    global $wp_admin_bar;

    // get node for New + menu node
    $new_content_node = $wp_admin_bar->get_node('new-content');

    // change URL for node, edit for prefered link
    $new_content_node->href = admin_url( 'post-new.php?post_type=portfolio' );

    // Update New + menu node
    $wp_admin_bar->add_node($new_content_node);

    // remove all items from New Content menu
    $wp_admin_bar->remove_node('new-post');
    $wp_admin_bar->remove_node('new-media');
    $wp_admin_bar->remove_node('new-page');
    $wp_admin_bar->remove_node('new-user');

    // add back the new Post link
    $args = array(
        'id'     => 'new-post',    
        'title'  => 'Blog Post', 
        'parent' => 'new-content',
        'href'  => admin_url( 'post-new.php' ),
        'meta'  => array( 'class' => 'ab-item' )
    );
    $wp_admin_bar->add_node( $args );

    // add back the new Page 
    $args = array(
        'id'     => 'new-page',    
        'title'  => 'Page', 
        'parent' => 'new-content',
        'href'  => admin_url( 'post-new.php?post_type=page' ),
        'meta'  => array( 'class' => 'ab-item' )
    );
    $wp_admin_bar->add_node( $args );

}

Here is my menu after:

This section rendered via wp_admin_bar_new_content_menu (admin-bar.php Line No. 690-746) callback on admin_bar_menu action (class-wp-admin-bar.php Line No. 584-588).

I couldn't find any hooks to filter the menu items here. So i'm thinking of removing that action and add another action with your own sorting.

Remove this action with remove_action and then attach your custom callback again to add menus. Eg.

remove_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );

add_action( 'admin_bar_menu', 'wpse273333_admin_bar_custom_new_content_menu', 70 );
function wpse_273333_admin_bar_custom_new_content_menu( \WP_Admin_Bar $wp_admin_bar ){
    // add your menus
    // check wp_admin_bar_new_content_menu() function for some idea
}
发布评论

评论列表(0)

  1. 暂无评论