[wp-hackers] Hooks, Actions, Filters

Will Anderson wp-hackers at itsananderson.com
Thu Jul 30 16:16:26 UTC 2009


On Thu, Jul 30, 2009 at 11:04 AM, Jeremy Clarke <jer at simianuprising.com>wrote:

> Just to quickly answer this other question. Some filters and actions
> pass multiple values so that you can also have information like
> page/post id. In this case its not available though.
>
> Luckily one thing that tends to work for situations like this is to
> globalize $post. If you're in the loop or on a single article page
> (whether in the admin or frontend) $post normally contains the full
> object for the post being worked on.
>
> So in your filter function you can probably do:
>
> global $post;
> echo $post->ID;
>
> Note that $post is used even if its a page.
>
> --
> Jeremy Clarke | http://jeremyclarke.org
> Code and Design | http://globalvoicesonline.org


The function in question (get_the_title) actually accepts an optional
argument which can specify the post ID, so while in most cases $post will
contain the ID of the post we're looking at, it's not necessarily safe to
assume this will ALWAYS be the case.

For example, suppose I'm listing related posts below a single post. If you
assume the $post object contains the ID of the post you're dealing with,
you'll always have the same ID, even though you'll be filtering different
titles.

If you need the post ID, it's probably better to attach your filter to the
"the_title" filter, which DOES pass the post's ID (per
http://core.trac.wordpress.org/browser/trunk/wp-includes/post-template.php?rev=11642#L120and
http://core.trac.wordpress.org/ticket/9666 ). From what you're trying to
accomplish, this is probably better anyway.


I also want to comment on the following code, which was provided earlier as
a solution for removing 'Private: ' from the post title.

      function remove_protected_title_format($s) {
              return str_replace('Protected: ', '', $s);
      }

The problem with this is that if the blog has localization set up, you
aren't guaranteed to have the "Protected: " text. It could easily be in
Spanish, French, or German (to name a few).

One solution might be to simply keep the format string, like so:

      function remove_protected_title_format($s) {
               return '%s';
      }

The problem with this of course is that if another plugin is trying to add
to the title, and that filter is run first, you'll overwrite it. It's still
probably safer to do it this way, though (I don't think many plugins will
try to add things here).

I actually submitted the patch to add these filters and, while they're
certainly better than nothing, they still seem like a bit of a kludge (as
evidenced by the issue I just enumerated). If anyone has a better idea for
how the "Private: " and "Protected: " tags might be added, such that they
can optionally be removed, that'd be awesome.

Here's the ticket for the patch I submitted:
http://core.trac.wordpress.org/ticket/8918

Best,
Will Anderson
http://www.itsananderson.com/


More information about the wp-hackers mailing list