• Resolved pauloc

    (@pauloc)


    Hi, congrats for this piece of art. I have a customer that ads page breaks <br> to some post titles. In the front end they work fine, but the document title shows them up. I’ve try to filter the title to strip those tags, but I’m not sure this is the best solution.
    Thanks in advance, Paulo.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @pauloc, hope you are doing well ?? I hope you understand, that <br> shouldn’t be in title — it is against the best HTML practices, not to mention 100% against SEO best practices. On the top of that, I don’t think it has anything to do with TSF.

    My guess is, your customer wants to break the title, so it looks nice on his device or for some other cosmetic reason. However, this is only the case for his display, and on other displays (phone/monitor) it has the exact opposite effect. Plus it hurts your SEO, as you HTML is not valid.

    So what your customer does is wrong, and (s)he should stop it. There are ways to break the title with css/js, maybe even some plugins. I hope you understand.

    Plugin Author Sybre Waaijer

    (@cybr)

    @lebaux is right, the title is meant for display as-is. I don’t agree with the current implementation of title tags within WordPress; the theme should handle and escape user input as-is, even though it’s deemed safe.

    For example, the editor wants to write about <span> tags… but how could he confidently annotate that when it’s not escaped? I’ll need to test and stress this behavior before Gutenberg arrives, so to have a uniform and predictable environment.

    Nevertheless, to resolve your issue without injecting code, you can simply add the “real” title in the SEO title meta field below the editor.

    If you’re using HTML in every page title, then I suggest looking for patterns and implement it within the theme.
    Alternatively, you can filter the WordPress title rendering with wp_strip_all_tags().

    For example, use this snippet (untested!!):

    add_action( 'init', function() {
    	add_filter( 'wp_title', 'wp_strip_all_tags', 8 );
    	add_filter( 'pre_get_document_title', 'wp_strip_all_tags', 9 );
    } );

    TSF tries to dominate over titles to maintain the aforementioned predictable environment, so that could be a reason your snippets didn’t work. The snippet I provided runs after said domination, so to circumvent it.

    Plugin Author Sybre Waaijer

    (@cybr)

    To remove the <br> tags too, use this snippet instead:

    add_action( 'init', function() {
    	add_filter( 'wp_title', 'my_wp_strip_all_tags_including_breaks', 8 );
    	add_filter( 'pre_get_document_title', 'my_wp_strip_all_tags_including_breaks', 9 );
    } );
    
    function my_wp_strip_all_tags_including_breaks( $input ) {
    	return wp_strip_all_tags( $input, true );
    }
    Thread Starter pauloc

    (@pauloc)

    Thank you both, and sorry for double posting (speedy fingers). I know my customer is doing wrong but, you know, customers… And I’ve also been surprised that the title isn’t HTML striped.
    The solution has been “add the “real” title in the SEO title meta”, something that didn’t come to my “developer” mind in first term and was the simplest solution.
    Thank you again, and congratulations for this great plugin.

    @pauloc customers are sometimes pain ?? Also, thank you for a kind review, it truly means a lot!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Strip HTML tags from title’ is closed to new replies.