@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.