DEV Community

loading...

A better WordPress admin title.

Adam Patterson
Husband, Father, Maker of things, writer of code, Cyclist, Eater of snacks.
Originally published at adampatterson.ca ・1 min read

At the moment I have 5 posts open to edit. Looking at them, all I see is "Edit Post < Adam Patterson - WordPress".

Sure 5 tabs aren't that many and not that hard to sort through but I began to wonder why WordPress wouldn't simply show the post title for pages and posts.

Lucky for me, it wasn't that hard.

First, we need to create a new filter that will be called when the admin page title is generated. This filter is called admin_title.

Next we will pull a few variables out of the global scope $post, $title, $action, $current_screen.

After we check that we are on the admin page and that the action is edit and not add. All that is left to do is make sure we are in the context of editing a post or a page. I supposed you could go farther by doing some experimenting here but this suited my needs just fine.

Lastly, structure your page title to suit you.

I ended up with "Edit Post < Post Name - Adam Patterson" or "Edit Page < Post Name - Adam Patterson".

I suppose the purists might want to keep WordPress in the title, but it's long enough that I don't see it as is so I am good without.

Simply add the following code to your themes functions.php file and you are all set!

add_filter('admin_title', 'better_admin_title', 10, 2);

function better_admin_title($admin_title, $title)
{
    global $post, $title, $action, $current_screen;
    if (is_admin() and $action == 'edit' and in_array($current_screen->id, ['post', 'page'])) {
        return $title . ' ‹ ' . $post->post_title . ' — ' . get_bloginfo('name');
    }

    return $admin_title;
}

Read more about admin_title

Discussion (5)

Collapse
bueltge profile image
Frank Bültge

Great idea for visibility, however a small hint - you should not add this to the functions.php of the Theme. A simple plugin should much more helpful because the topic is not in context with the front-end so that should be independent from the Theme.

Collapse
adampatterson profile image
Adam Patterson Author

Good point, I tossed it in there as a proof of concept but could easily add it to a function and I will :)

I like your comment on the Context of Fron End Dev, How do you feel about how Gutenberg blocks are created for custom themes?

Collapse
bueltge profile image
Frank Bültge

It more in the context of the theme. If the block is only for design, formatting so should it leave inside the Theme. However is also possible as plugin, because so it is possible to use after a switch of the theme or separately. But is the block a part of data, they're stored inside the database and have more a topic of data, not only design so should it a separate plugin, not inside the theme. It makes also much more easy to update them.

Collapse
tammalee profile image
Tammy Lee

This was a great idea! Awesome!

Collapse
adampatterson profile image
Adam Patterson Author

Thanks Tammy!