[wp-trac] [WordPress Trac] #39848: New filter 'the_title_wrap'

WordPress Trac noreply at wordpress.org
Sat Feb 11 20:46:23 UTC 2017


#39848: New filter 'the_title_wrap'
-------------------------------+-----------------------------
 Reporter:  milana_cap         |      Owner:
     Type:  feature request    |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  4.7.2
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 == Problem:

 In plugin I wish to add some markup after/before the post and page title
 so, naturally, I use `the_title` filter. However, this filter is applied
 on `get_the_title()` function which is used by all instances of post title
 all over the page (menus, edit links etc). On top of that, more and more
 themes are using `the_title()` with before and after params which makes
 all my custom markup wrapped inside this before and after. So when theme
 is using `the_title( '<h1 class="entry-title">', '</h1>' )`, and my markup
 in filter is, for example an image, then I get:

 {{{
 <h1 class="entry-title">The title <img src="image.jpg" ></h1>
 }}}

 Or something even worse if my custom markup is more complex.

 == Proposal (possible solution):

 I have tested another filter on `the_title()` function **(wp-includes
 /post-template.php)** and it gave me exactly what I needed:

 {{{#!php

 /**
  * Display or retrieve the current post title with optional markup.
  *
  * @since 0.71
  *
  * @param string $before Optional. Markup to prepend to the title. Default
 empty.
  * @param string $after  Optional. Markup to append to the title. Default
 empty.
  * @param bool   $echo   Optional. Whether to echo or return the title.
 Default true for echo.
  * @return string|void Current post title if $echo is false.
  */
 function the_title( $before = '', $after = '', $echo = true ) {
         $title = get_the_title();

         if ( strlen($title) == 0 )
                 return;

         $title = $before . $title . $after;

         /**
          * Filters the post title after 'the_title' filter.
          *
          * @param string $title The post title.
          */
         $title = apply_filters( 'the_title_wrap', $title );

         if ( $echo )
                 echo $title;
         else
                 return $title;
 }

 }}}

 I named it `the_title_wrap` because it wraps everything that comes with
 `the_title` function and filter. This filter doesn't apply on menus, edit
 links etc. Tested on twenty* themes, it applies only on posts and pages
 title on singulars and on posts title inside the loop on archive pages.
 Also, my custom markup doesn't end up inside `<h1>` or `<a>` tags. Now it
 looks like this:

 {{{
 <h1 class="entry-title">The title</h1><img src="image.jpg" >
 }}}

 This is, of course, somewhat uncertain because I'm counting on theme
 author to use `the_title()` instead of `get_the_title()` but I think it's
 worth of effort as sometimes it becomes nearly impossible to target only
 entry title on singular or inside loop on archives etc.

 Thank you.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39848>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list