[wp-hackers] is it possible to change the_title halfway into the processing of a page/post?

Mike Little wordpress at zed1.com
Thu Feb 23 16:02:20 UTC 2012


On Thu, Feb 23, 2012 at 15:42, Haluk Karamete <halukkaramete at gmail.com>wrote:

> I will consider your valuable ideas...   but I was still wondering
> what would be a problem if we'd go with an "ob_start" implementation,
> that is straight forward.
>
> Here is one plan...
>
> Create a a plugin ( that hooks to the init ) whose job is to detect if
> the page url contains one of those querystring variables that my
> framework uses.
>
> If the url is such, then the plugin runs the ob_start. ( Now we know,
> we can have control over the_title whenever we want to. )
>
>

Absolutely the *wrong* way to do it!

If you hook around template_redirect or earlier, then you can check the
query string and set up a hook on wp_title without having to do any
buffering or search and replace. Instead of being able to change what
wp_title outputs, you simply make it output the correct thing.
Similarly you can hook on the_title and the_content, again no search and
replace needed.

Using output buffering you are hoping to modify what WordPress has already
done to change it after the fact and make it say what you want.

Using the methods I've already described, you just make it say what you
want in the first place.



> With that in mind, in the the_content, we use exec_php to do whatever
> we want to do, that is contacting the db's getting the current title
> for that recid and so on. Obviously, all this code is stored in the
> wp_posts and therefore, theme independent.
>
>
All the code in a plugin is theme independent too. Seriously, there has
been no need to use exec_php for several years!



> And then we take an advantage the fact that the ob_start is on and
> therefore, we use the ob_get_contents(), do a replacement on the
> already computed html and page title, and finally use ob_end_flush()
> to seal the deal.
>
>
As above, just plain wrong.



> Could you comment what the issues would be with this approach?
>
>
Did you look at my code? It actually works, I spent the time to give you a
working example. It's not pseudo code.


Mike
-- 
Mike Little
http://zed1.com/


More information about the wp-hackers mailing list