For anyone who has experience in building WordPress themes – or even just one theme – or anyone who has experience in working with child themes, or simply modifying a core theme, then you’re more than likely with
<a title="wp_title" href="http://codex.wordpress.org/Function_Reference/wp_title" target="_blank">wp_title</a>.
It’s one of the tags in WordPress that’s easy to usd and that’s easy to understand. Straight from the Codex:
Displays or returns the title of the page. A separator string can be defined, and …, that separator can be designated to print before or after the title of the page.
This tag can be used anywhere within a template as long as it’s outside The Loop on the main page, though is typically used in the <title> element for the head of a page.
Not much to it, right?
But it’s also one of the tags that can be abused which can cause problems especially as it relates to plugins.
Filtering wp_title Matters
First, the default use case for
wp_title is quite simple. It’s easy enough to add that tag, declare the separate, and how you want the title and the separator to be displayed.
<title><?php wp_title( '|', true, 'right' ); ?></title>
Easy enough – but it can always get worse (and I know this from personal experience).
Don’t Do It This Way
For example, let’s say that you want to create a more elaborate format that displays titles across your blog.
You could do something like this:
Which is really ugly, and really wrong. And I’ll be the first to admit that this is code that I pulled from something I’ve written before (just goes to show that you do learn with time :)
The problem with this is that you’re not able to filter
wp_title. More specifically, this means that plugin developers who offer to customize your theme’s title structure can’t do it because you’re not properly using
Instead, there’s a mishmash of
get_bloginfo, and string concatenation in order to get the scheme you’re going for, and a good rule of thumb in programming – at least in the context of some frameworks and applications – is that if it feels weird to write it in a certain way, then there’s likely a better way to do so.
Do It This Way
wp_title is a filtered function, this means that we’re able to provide a custom hook that allows us to define the schema for displaying our titles not only more precisely, but also correctly.
And by that, I mean that we’re not using a combination of “blog info” functions and the like – we’re taking advantage of how WordPress is built: through the event-driven paradigm.
On top of that, this will allow plugin developers to remove our custom code and insert theirs. Everyone wins, right?
To do this, you first need to define exactly how you want the title to appear using the basic
Next, you define a custom function, hook it to
wp_title, and provide the code necessary for filtering the information:
Not only do you gain all of the advantages that are mentioned above, but you’re also doing this in a more correct way.
And, like the first bit of code, this is code straight from a project I’m working on right now.
I mention this not to showcase what I’m doing (after all, the code’s gonna be open sourced anyway), but to showcase that there are going to be times where you write bad code, times where you write good code, and times where you’re going to have something that works but could probably be improved.
So with that said, there’s likely something that can even be critiqued in the code above. But the point is that what I’m showing is what I’ve once done, where I am now, and the advantages of using available hooks.
Critique away :).