Make WordPress Core

Opened 8 years ago

Last modified 7 years ago

#39848 new enhancement

New filter 'the_title_wrap'

Reported by: milana_cap's profile milana_cap Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.2
Component: Posts, Post Types Keywords: has-patch 2nd-opinion
Focuses: template Cc:

Description

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:


/**
 * 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.

Attachments (3)

post-template.php (57.1 KB) - added by milana_cap 8 years ago.
39848.patch (813 bytes) - added by milana_cap 7 years ago.
Proposal for new filter on title after the_title filter
39848.2.diff (529 bytes) - added by donmhico 5 years ago.
Refreshed the patch.

Download all attachments as: .zip

Change History (5)

#1 @milana_cap
8 years ago

  • Type changed from feature request to enhancement

@milana_cap
7 years ago

Proposal for new filter on title after the_title filter

#2 @milana_cap
7 years ago

  • Focuses template added
  • Keywords has-patch 2nd-opinion added

@donmhico
5 years ago

Refreshed the patch.

Note: See TracTickets for help on using tickets.