Is possible to change page title on the fly from plugin?
I’ve try global $post, but seem like plugin runs after.
Any Ideas?
Edit:
Im writting some pages on the fly, based on same page / post, so every page show the same title.
Looking a way to do via shortcode or writting my own plugin/function
There’s a filter for that:
If you want to alter the “Protected” and “Private” titles, then you need other filters:
Last but not least, you must add your filter callbacks early enough.
This depends on the context your page title is being rendered in and how the data is being fed into it.
Consider the following:
Solution would involve hooking to the
the_title
filter and alter it in a breeze.How about this?
And it’s not nice due to the problems with altering it. It’s hard to know how the
$post
got populated in the first place. Consider:No place to hook there, at first glance. But it can be done inside the actual posts query. Check out the
found_posts
filter.However, consider the following:
That’s insane.
Point is, depending on your situation there’s probably a good solution. Check the template file, see how the title is being rendered. Fix it to be comfortable or make your plugin work a little harder.
And you will never ever be able to alter the title via a plugin if
mysql_query()
is used directly to retrieve the title. Yes, I’ve actually seen this done. Some people go as far as hard code it in the template even. Like front-page.php will contain<h2>Home</h2>
and never even use the$post->post_title
or ever query the database.So, depends on the context and the situation.
I’m building a plugin that is heavily reliant on a front end shortcode. This shortcode is responsible for gathering data which I need to use in the page tab
<title>
tag. Using a shortcode made it difficult to tie in with the wordpress hooks and filters so I ended up using JavaScript to sort out the page tab title.Easily done like this:
And to be really good I also tidied up the DOM by removing any other
<title>
elements:I actually echoed this out in PHP which made it simpler to add my PHP variables into the mix:
Hope this helps someone 🙂