[wp-hackers] Hooks, Actions, Filters

Will Anderson wp-hackers at itsananderson.com
Fri Jul 31 12:49:58 UTC 2009


On Fri, Jul 31, 2009 at 7:37 AM, scribu <scribu at gmail.com> wrote:

> You could just write
>
> return str_replace(__('Protected:'), '', $format);
>
> to account for l10n.
>
> --
> http://scribu.net


But if they're using a localized version of WordPress, that requires your
plugin to not only be translated, but translated with the same text.

Additionally, what if the localization translated the string to something
like 'zaza %s sese'? The solution still won't work, regardless of whether
your plugin is localized or not.

What I'm wondering is if the "Protected" and "Private" code should be moved
to separate functions which are attached to the the_title filter. Here's one
way it could be done:

function get_the_title( $id = 0 ) {
        $post = &get_post($id);

        $title = $post->post_title;

        if ( !is_admin() ) {
                if ( empty($post->post_password) ) {
                    remove_filter('the_title', 'format_protected_title');
                } else if ( !isset($post->post_status) || 'private' !=
$post->post_status ) {
                    remove_filter('the_title', 'format_private_title');
                }
        }
        return apply_filters( 'the_title', $title, $post->ID );
}

function format_protected_title( $title, $id ) {
    $protected_title_format = apply_filters('protected_title_format',
__('Protected: %s'));
    return sprintf($protected_title_format, $title);
}

function format_private_title( $title, $id ) {
    $private_title_format = apply_filters('private_title_format',
__('Private: %s'));
    return sprintf($private_title_format, $title);
}

The second two functions would be added as filters on 'the_title' elsewhere
(is there a file somewhere that adds all the core filters?)

The issue here is that if a plugin author called the 'the_title' filter on a
post title without removing those two functions, they'll get something like
'Protected: Private: Post Title', regardless of the post's privacy status.

Here's a second solution that has a bit more overhead, but doesn't have the
problem just described.

function get_the_title( $id = 0 ) {
        $post = &get_post($id);

        $title = $post->post_title;

        return apply_filters( 'the_title', $title, $post->ID );
}

function format_protected_title( $title, $id ) {
    $post = &get_post($id);

    if ( !is_admin() ) {
        if ( !empty($post->post_password) ) {
            $protected_title_format =
apply_filters('protected_title_format', __('Protected: %s'));
            return sprintf($protected_title_format, $title);
        }
    }
}

function format_private_title( $title, $id ) {
    $post = &get_post($id);

    if ( !is_admin() ) {
        if ( isset($post->post_status) && 'private' == $post->post_status )
{
            $private_title_format = apply_filters('private_title_format',
__('Private: %s'));
            return sprintf($private_title_format, $title);
        }
    }
}

The problem here is that we're *always* getting the post object 3 times. I'm
not sure whether that's cached by WordPress, but it's got to have a
performance impact. All the same, it's probably a slightly safer solution.

Thoughts?

-- 
Will Anderson
http://www.itsananderson.com/


More information about the wp-hackers mailing list