Tonight the WordPress tinkering I was doing was way down in the weeds, but it never fails me how the codebase let’s you manipulate almost every thing in the interface.
So a current project I am doing has a custom post type for something called Artifacts (yes, it’s for a portfolio project). I was showing my client’s how they could use the WordPress admin bar (the black menu site owners have where-ever they go) to easily create a new one via the + New menu at the top:
They like this, but asked something I would bot have thought of since I am a little too familiar with the interface–why can’t Artifact be at the top of this menu, as its the most common thing users will be doing?
That’s a good question.
And while I am looking at it, almost none of them need the shortcuts to New Media or New User.
As usual I found a similar question in Stack Exchange, but as it rarely happens, the answers were not too helpful.
My own digging got me in the corner of the WordPress Codex about WP_Admin_Bar. Apparently, everything on this menu is considered a “node”. I played a bit with the get_node() function thinking I could get the thing as an object I could modify and stick back.
Dead end.
So I went a bit more brute force. There is a remove_node() method to take things off, and add_node() to put things up.
My task then was to remove everything but the Artifact item my post_type creates, then put back the ones I want. In order.
You need access to the CSS ids for the menu items, so the browser Developer Inspector comes in handy to fetch the entire menu.
1 2 3 4 5 6 |
<ul id="wp-admin-bar-new-content-default" class="ab-submenu"> <li id="wp-admin-bar-new-post"><a class="ab-item" href="http://twu.test/wp-admin/post-new.php">Post</a> </li> <li id="wp-admin-bar-new-media"><a class="ab-item" href="http://twu.test/wp-admin/media-new.php">Media</a> </li> <li id="wp-admin-bar-new-page"><a class="ab-item" href="http://twu.test/wp-admin/post-new.php?post_type=page">Page</a> </li> <li id="wp-admin-bar-new-twu-portfolio"><a class="ab-item" href="http://twu.test/wp-admin/post-new.php?post_type=twu-portfolio">Artifact</a> </li> <li id="wp-admin-bar-new-user"><a class="ab-item" href="http://twu.test/wp-admin/user-new.php">User</a> </li></ul> |
The parent menu has an id of new-content
and the menu item for New Post has one of new-post
(you look at the list item ids and remove the wp-admin-bar
is that obscure or what>).
To make this happen, my theme’s functions.php meeds a hook for the action wp_before_admin_bar_render as we are mucking around with the admin bar before it is assembled
1 |
add_action( 'wp_before_admin_bar_render', 'portfolio_adminbar' ); |
My custom function first goes through and removes the nodes for New Post, Page, Media, and User.
Then I put back the ones I want. Note the use of the admin_url()
function to create the URL for the site’s dashboard, and also, I can make the menu item for Post be Blog Post
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
function portfolio_adminbar() { // admin bar needs to be known global $wp_admin_bar; // 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 ); } |
It’s rather brute force, but it works. Now the theme has a simpler +New menu, and the Artifact one is first,
Tiny details, tiny ones. I dig ’em.
Just to be a good StackExchange citizen, I posted my solution.
Featured Image: Added a screenshot of part of the WordPress Admin bar (screenshot from my development site) to Shark steam mop on white wall and wood floors flickr photo by yourbestdigs shared under a Creative Commons (BY) license
It may be too late and is broader than this goal but here’s a chunk of stuff about removing things from WP. I found the same thing you did. Seems this isn’t a common pattern but it is a useful one and I hope this adds connections between the islands of documentation.