Fixing WordPress bad new lines (extra br tag) when copy-pasting HTML by modifying wpautop() behavior

When you copy-paste arbitary HTML code into HTML view in WordPress post editor, you’ll get HTML soft newlines converted to hard newlines (<br>)  by default. This is utterly annoying for me, who writes posts offline in reStructuredText and then copies the content to WordPress thru rst.ninjs.org or rst2html.py.

I finally found some time to workaround this issue. The culprint is wpautop() filter.

Add the following in functions.php thru the theme editor:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

// Preserve <p> tag and do not insert <br> when transforming HTML for display
function wpautop2($pee) {
 return wpautop($pee, false);
}

add_filter( 'the_content', 'wpautop2' );
add_filter( 'the_excerpt', 'wpautop2' );

 Subscribe to RSS feed Follow me on Twitter Follow me on Facebook Follow me Google+

One thought on “Fixing WordPress bad new lines (extra br tag) when copy-pasting HTML by modifying wpautop() behavior

Leave a Reply

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