I’m using a translation plugin which creates a hook for the_title, the_content and other things.
All is working fine except one bit of code that does not display the title.
It uses this code:
$page_title = apply_filters('the_title',get_the_title());
If i try to use get_the_title() or the_title(), it breaks.
What does apply filters do, and how do i make it not skip the hook from the translation plugin?
the_titleandthe_contentalso exist in the WordPress core. They are utilized for many things. Why a line like that is useful is apparent if you know what hooks are.Filter hooks and Action hooks are essentially laundry lists. You can put functions on a hook, one after another so they form a queue, and when this hook is called on (by
do_actionandapply_filtersrespectively) WordPress will unqueue one function after another. As it does, it will execute them.The difference between actions and filters is that while they can both accept values, only filters will return a modified value. Actions do something that is important in and of itself; filters take a value and return a modified version of it that can be used later on. For example, to capitalize every title that’s printed with
the_titlewe can use the following piece of code:Since we know that all functions hooked on
the_title– the hook and not the function – will be executed only byapply_filterswe expect to find it somewhere in functionthe_title. Actually that function is basicallyecho get_the_titleand here’s howget_the_titlelooks:I’m posting the entire function because learning to look for hooks in the source code is of utmost importance for bourgeoning WordPress developers. The source code is littered with hooks, and so they can be used to modify many aspects of the built-in functions of WordPress. Now that you’ve located
apply_filters( 'the_title', ... )in the source code, you can appreciate its importance!the_titlesimply echoes the value given to it byget_the_titleand you can modify or even replace the value thatget_the_titlereturns by attaching a filter to the hookthe_title!Now, I hope you don’t think all that I’ve written so far is gratuitous. In fact, now we can easily answer your main question which was “why does it not work?”
First of all, you can never pass
the_titleto a function! It would be like writingsomefunction( $var1, echo $var2, $var3 ). We cannot pass a value to a function by using echo, because echo sends its output to the browser.The better attempt is the one you posted
But as we’ve seen,
get_the_titlehas appliedthe_titleto its return value already. You’re simply applying all those functions a second time. It could result in strangeness if you have some custom filters attached tothe_titleor it could do nothing. So it either muddles the result or is gratuitous. Which is why you should simply do this:Now, you also said
This is confusing, because we would not expect a variable assignment to output anything! To output the title, you could do this
But as we’ve learned, this is really (look at the source code for the slight difference) the same as:
So I wrote quite a lot just to come to the conclusion that you probably want to use
the_titleon its own. But I hope this can be a good resource on filter/action hooks as well.Any questions are welcome.