WordPress

Stop WordPress Automatically Adding br

WordPress uses a function called wpautop to automatically add paragraphs and line breaks to your content from the content editor. This will change double line breaks and convert them into a HTML paragraph.

<?php wpautop( $foo, $br ); ?>

This takes two parameters the first being the text that needs to be converted, the second being a boolean value, defaults to true which will take line breaks and convert these to line breaks.

There are certain situations in your editing where you want to stop WordPress automatically adding line breaks in your content. To do this you need to replace the default wpautop and pass in a false as the second parameter to stop WordPress adding the HTML line breaks (<br />).

Add the following to your functions.php file to stop WordPress adding the line break.

// Remove the default filters
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

// Add the new function to be the the_content and the_excerpt filter
add_filter( 'the_content', 'pu_wpautop_without_br' , 99);
add_filter( 'the_excerpt', 'pu_wpautop_without_br' , 99);

/**
 * Create a new function that uses the default wpautop function passing in a false as the second parameter to stop adding line breaks
 */
function pu_wpautop_without_br( $content ) 
{
    return wpautop( $content, false );
}
Back to top

Leave a Reply

Your email address will not be published. Required fields are marked *