Opened 10 years ago
Last modified 13 months ago
#39848 new enhancement
New filter 'the_title_wrap'
| Reported by: | milana_cap | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Posts, Post Types | Version: | 4.7.2 |
| Severity: | normal | Keywords: | has-patch 2nd-opinion |
| Cc: | Focuses: | template |
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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Proposal for new filter on title after the_title filter