How do I turn off self-closing tags for markup in WordPress (for HTML5, or HTML4, for example)?

I want to use HTML5 in my WordPress theme, how do I turn off wptexturize?

I don’t mind WP adding breaks, but I want them to be <br> and not <br />. How do I get control over how those breaks show up in my code?

EDIT: I only really care about the <br> tag issue, I don’t mind the typographic changes it makes.

EDIT2: Actually, I guess <img> tags matter too. Any self-closing standalone tags will matter here. So, <hr> might be an issue as well. Not to mention such wp_head() items as <link> and various <meta> tags.

6

Line breaks are added by wpautop(), not wptexturize(). wpautop() is also the function that automatically adds paragraph tags.

You’re better off fixing the <br />‘s than you are replacing the filter. Since wpautop() runs at priority 10, you can just hook in after that and fix it.

add_filter( 'the_content', 'html5_line_breaks', 25 );

function html5_line_breaks( $content ) {
    return str_replace( '<br />', '<br>', $content );
}

Edit after OP update:

WordPress functions are designed to output XHTML. In order to get rid of those trailing slashes site-wide, you’re going to have to use an output buffer. You could use a filter similar to the one above to replace slashes in the post contents, but that wouldn’t catch your head, sidebar, etc.

It’s a bit ugly and might have a small impact on performance, but here you go (drop this in a plugin or your theme’s functions.php file):

if ( !is_admin() && ( ! defined('DOING_AJAX') || ( defined('DOING_AJAX') && ! DOING_AJAX ) ) ) {
    ob_start( 'html5_slash_fixer' );
    add_action( 'shutdown', 'html5_slash_fixer_flush' );
}

function html5_slash_fixer( $buffer ) {
    return str_replace( ' />', '>', $buffer );
}

function html5_slash_fixer_flush() {
    ob_end_flush();
}

That code says if you’re not in the administration area and not doing an AJAX request handling, then start buffering the output through a filter and then using the WordPress shutdown hook, output that buffer.

Leave a Comment