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.

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

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

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

If this kind of stuff has any value, please support me monthly on Patreon or a one time PayPal kibble toss
Profile Picture for Alan Levine aka CogDog
An early 90s builder of the web and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *