0

So I have plugin that pulls remote data (real estate listings) into a wordpress site. I have been asked by a client to allow the page title for each listing to be set based upon that information and formatted the way they want. I can use the_title filter to change the content of the title easily enough, but I am running into a problem with the $before and $after elements impacting the formatting. For example, twentyfourteen uses this to output a page title:

the_title( '<header class="entry-header"><h1 class="entry-title">', '</h1></header><!-- .entry-header -->' );

So if I run the_title filter and do something like this (greatly simplified):

add_filter( 'the_title', 'fls_mls_listing_title_filter', 30, 2 );
function fls_mls_listing_title_filter( $title, $id ) {
   if ( in_the_loop() ) {
      $new_title = 'Something: <h1>Main Title</h1><p>Subtitle</p>'
      return $new_title;
   }   
   return $title;
}

I end up with this:

<header class="entry-header">
  <h1 class="entry-title">Something</h1>
  <h1>Main Title</h1>
  <p>Subtitle</p>
</header><!-- .entry-header -->

But I would like to end up with this:

Something
<h1>Main Title</h1>
<p>Subtitle</p>

Obviously this is not a very logical example, but I wrote it just to keep it simple. I need to do this from within the plugin and without creating a custom page template so it will work with any theme if possible. Any insights would be greatly appreciated.

1 Answer 1

1

There is no filter that would allow you to strip those $before and $after values:

42    function the_title($before = '', $after = '', $echo = true) {
43            $title = get_the_title();
44    
45            if ( strlen($title) == 0 )
46                    return;
47    
48            $title = $before . $title . $after;
49    
50            if ( $echo )
51                    echo $title;
52            else
53                    return $title;
54    }

https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/post-template.php#L42

That is the end of the story. To change that you need to hack the theme. What you want, "to do this from within the plugin and without creating a custom page template so it will work with any theme", isn't possible.

At best you could write output buffering into the theme, but again, you'd need to hack the theme.

Even if it were possible, you would like cause numerous and sometimes catastrophic failure in the theme, as you would be removing markup that the theme may depend upon. What you want is a bad idea, sorry.

1
  • not the answer I was looking for, of course, but the answer I had expected as I had looked at the same code snippet. Thanks for your help!
    – MatthewLee
    Mar 14, 2014 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.