[theme-reviewers] Usage of wp_title filter.

Edward Caissie edward.caissie at gmail.com
Wed Oct 17 15:14:27 UTC 2012


Most everyone has their own way of writing this function, but this is what
I use (or a slight variation) in all of my themes:

        /**
         * Use as filter input
         *
         * @param   string $old_title - default title text
         * @param   string $sep - separator character
         * @param   string $sep_location - left|right - separator placement
in relationship to title
         *
         * @return  string - new title text
         */
        function dmm_wp_title( $old_title, $sep, $sep_location ) {
            global $page, $paged;
            /** Set initial title text */
            $dmm_title_text = $old_title . get_bloginfo( 'name' );
            /** Add wrapping spaces to separator character */
            $sep = ' ' . $sep . ' ';

            /** Add the blog description (tagline) for the home/front page
*/
            $site_tagline = get_bloginfo( 'description', 'display' );
            if ( $site_tagline && ( is_home() || is_front_page() ) )
                $dmm_title_text .= "$sep$site_tagline";

            /** Add a page number if necessary */
            if ( $paged >= 2 || $page >= 2 )
                $dmm_title_text .= $sep . sprintf( __( 'Page %s',
'desk-mess-mirrored' ), max( $paged, $page ) );

            return $dmm_title_text;
        }
        add_filter( 'wp_title', 'dmm_wp_title', 10, 3 );

The point being not so much the code in this case but the fact I am not
seeing duplicate titles *and* I am not testing for `is_feed` either.


Cais.


On Wed, Oct 17, 2012 at 10:29 AM, WPForever Team <hi at wpforever.com> wrote:

> Yes, Philip is correct.
>
> This is how TwentyTwelve does it -
> http://core.trac.wordpress.org/browser/trunk/wp-content/themes/twentytwelve/functions.php#L168
>
>
> Philip M. Hofer (Frumph) wrote:
>
>
> That is precisely how to do it. But you should also return the
> original $title being passed through the filter
>
> This is how I do it (not totally pretty), notice the check for the
> feed if it is the feed it returns the original $title .. I probably
> could have gotten away with just doing = .= with $title in the
> !is_feed() section ;/
>
> function easel_filter_wp_title( $title ) {
> global $wp_query, $s, $paged, $page;
> if (!is_feed()) {
> $sep = __('&raquo;','easel');
> $new_title = get_bloginfo('name').' ';
> $bloginfo_description = get_bloginfo('description');
> if ((is_home () || is_front_page()) && !empty($bloginfo_description)
> && !$paged && !$page) {
> $new_title .= $sep.' '.$bloginfo_description;
> } elseif (is_single() || is_page()) {
> $new_title .= $sep.' '.single_post_title('', false);
> } elseif (is_search() ) {
> $new_title .= $sep.' '.sprintf(__('Search Results: %s','easel'),
> esc_html($s));
> } else
> $new_title .= $title;
> if ( $paged || $page ) {
> $new_title .= ' '.$sep.' '.sprintf(__('Page: %s','easel'),max( $paged,
> $page ));
> }
> $title = $new_title;
> }
> return $title;
> }
>
> add_filter( 'wp_title', 'easel_filter_wp_title' );
>
> -----Original Message----- From: esmi at quirm dot net
> Sent: Wednesday, October 17, 2012 7:16 AM
> To: theme-reviewers at lists.wordpress.org
> Subject: [theme-reviewers] Usage of wp_title filter.
>
> I've just been reviewing the Codex page on the wp_title() filter:
>
> <http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_title>
>
> Specifically the example quoted as best practice. When I've tried to
> implement that function, it generates a duplicated title in the RSS
> feed. This can be circumvented by changing:
>
> return get_bloginfo( 'name' ) . $page_type . $title . $page_num;
>
> to:
>
> if( !is_feed() ) return get_bloginfo( 'name' ) . $page_type . $title .
> $page_num;
>
> but I'm not 100% convinced that this is the proper usage of is_feed().
>
> Anyone care to comment before I amend the Codex page?
>
> Mel
>
>
> _______________________________________________
> theme-reviewers mailing list
> theme-reviewers at lists.wordpress.org
> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wordpress.org/pipermail/theme-reviewers/attachments/20121017/7fdc3eda/attachment.htm>


More information about the theme-reviewers mailing list