[wp-trac] [WordPress Trac] #26667: Improve filter in single_term_title()

WordPress Trac noreply at wordpress.org
Tue Dec 17 19:20:48 UTC 2013


#26667: Improve filter in single_term_title()
--------------------------+-----------------------------
 Reporter:  clifgriffin   |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  3.8
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 There are some oddities to how taxonomy titles are handled in
 `wp_title()`.

  For categories and tags, the title is set to the current term title,
 which is desirable in my opinion:
 {{{
         if ( is_category() || is_tag() ) {
                 $title = single_term_title( '', false );
         }
 }}}

 But for all other taxonomies, the taxonomy label is injected into the
 title:
 {{{
         if ( is_tax() ) {
                 $term = get_queried_object();
                 if ( $term ) {
                         $tax = get_taxonomy( $term->taxonomy );
                         $title = single_term_title( $tax->labels->name .
 $t_sep, false );
                 }
         }
 }}}

 This seems oddly specific and not the typical WordPress way, in my
 opinion. It seems to assume a lot in terms of what users will want.
 Ideally I'd take this block completely out and simply add is_tax() to the
 first block.

 Being a pragmatist though, I will assume no one will take me up on that
 particular solution. :)

 This really gets messy when you get into `get_term_title()`.  The filters
 provided in this function do not filter the result of the operations but
 rather the input, which is a bit curious given the normal usage of
 filters.

 The code:
 {{{
 function single_term_title( $prefix = '', $display = true ) {
         $term = get_queried_object();

         if ( !$term )
                 return;

         if ( is_category() )
                 $term_name = apply_filters( 'single_cat_title',
 $term->name );
         elseif ( is_tag() )
                 $term_name = apply_filters( 'single_tag_title',
 $term->name );
         elseif ( is_tax() )
                 $term_name = apply_filters( 'single_term_title',
 $term->name );
         else
                 return;

         if ( empty( $term_name ) )
                 return;

         if ( $display )
                 echo $prefix . $term_name;
         else
                 return $prefix . $term_name;
 }
 }}}

 So, my recommended solution is to simply add a filter here for the result
 which passes in the `$prefix` in case we would like to remove it.

 Like so:
 {{{
 function single_term_title( $prefix = '', $display = true ) {
         $term = get_queried_object();

         if ( !$term )
                 return;

         if ( is_category() )
                 $term_name = apply_filters( 'single_cat_title',
 $term->name );
         elseif ( is_tag() )
                 $term_name = apply_filters( 'single_tag_title',
 $term->name );
         elseif ( is_tax() )
                 $term_name = apply_filters( 'single_term_title',
 $term->name );
         else
                 return;

         if ( empty( $term_name ) )
                 return;

         $result = apply_filters( 'single_term_title_result', $prefix .
 $term_name, $term_name, $prefix );

         if ( $display )
                 echo $result;
         else
                 return $result;
 }
 }}}

 The attached patch accomplishes this.

 However, I think more sweeping changes might be warranted here.

 Clif

--
Ticket URL: <http://core.trac.wordpress.org/ticket/26667>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list