Need some help?

I'm usually available for small jobs or problem solving with jQuery or css. Just get in touch.

Plugin to stop WordPress adding <br/> tags

Quite a while ago I wrote a blog post about stopping WordPress adding <br/> into your posts, a particularly irritating habit. The solution I posted involved editing the function wpautop() in formatting.php, specifically changing this:

function wpautop($pee, $br = 1)

to this:

function wpautop($pee, $br = 0)

Now, this works fine, but whenever you upload a new version of WordPress, the change gets overridden and you have to do it again. If you’re interested, here’s the detail. wpautop() most noticeably adds <p> tags around (what it thinks are) paragraphs, as well as adding the <br/> and a host of other formatting stuff. It’s easy enough to get rid of the function altogether via:

remove_filter('the_content','wpautop');

However, I didn’t want to disable the paragraph formatting, particularly on a site for a client, just sort the <br/>. So here’s the whole plugin code:

<?php
/*
Plugin Name: Better wpautop 
Plugin URI: http://www.simonbattersby.com/blog/plugin-to-stop-wordpress-adding-br-tags/
Description: Amend the wpautop filter to stop wordpress doing its own thing
Version: 1.0
Author: Simon Battersby
Author URI: http://www.simonbattersby.com
*/

function better_wpautop($pee){
return wpautop($pee,$br=0);
}

remove_filter('the_content','wpautop');
add_filter('the_content','better_wpautop');
?>

All I'm doing is defining a function better_wpautop() which calls the existing function, setting the $br variable to 0

Feel free to use this. It'd be great to know if it works for you so leave me a note below. To add as a plugin, download the zip file, unzip in your plugins folder and activate via the admin panel

Better wpautop plugin

To install the plugin, download the zip file by clicking on the icon, unzip, load into your WordPress plugins folder and activate.

Better wpautop plugin

17 responses to “Plugin to stop WordPress adding <br/> tags”

  1. Thanks for this. I put the code in my functions.php file and it work perfectly. I appreciate it.

  2. Chris says:

    thanks. solved in two minutes a problem I might have taken hours to find and fix (also – thanks JG @1 – I also just dumped the code in functions.php)

  3. Charles says:

    Thanks! This was driving me crazy! I’m running WP 3.1.4

  4. Paul says:

    Now that I have installed Better Wpautop, I realized that the visual editor is stripping our paragraphs and line breaks. Is there a suggested change to fix the visual editor?

  5. Simon says:

    The WP visual editor does fight back, yes. I have mine disabled completely, but if this isn’t an option, I’m not clued up on any fixes I’m afraid.

  6. David says:

    AWESOME!! Fixed it for me too, I added the code to functions.php and it works perfectly! Thank you :) – WP3.2.1

  7. Brian FitzGerald says:

    Worked wonders for me, thanks for posting this solution!

    - WordPress 3.2.1

  8. Ahmad Humeid says:

    It worked! I think this solves a big issue for me. Thank you!

  9. JMac says:

    Thanks a lot for this! Just learning about WordPress and this was really pissing me off as well!!

  10. Sophie says:

    Is this still working with WP 3.3.1? I included it in my theme’s function.php and so far I don’t see any difference…

  11. Simon says:

    Yes, just tested it and it works fine.

  12. bron says:

    I have diable visual editor plugin enabled and visual editor turned OFF AND using this plugin and still it adds BR to my posts why?

    It is because this only work WITH the visual editor ON

  13. Simon says:

    Hi Bron

    It works OK with the visual editor disabled. I use this approach myself, but just have visual editor disabled against my user, rather than via a plugin.
    You could try adding the code to your theme’s functions.php and see if that works for you. Possibly something in your visual editor disabling plugin is interfering.

  14. Awais says:

    I am using WordPress version 3.3.1 and i tried this hack and it did not work for me. Is it just me or someone also getting this problem? :(

  15. Simon says:

    It works fine for me in 3.3.1 – previous commenter Sophie also got it working OK.

  16. Ricky says:

    This works great! 3.3.1. WP added 10 br tags to every page I created.

  17. John Clarke says:

    I added this (including your credits) into my functions.php file and it works perfectly.

    Thank you very much for publishing a simple solution to a problem that has been bugging a lot of people for a very long time.

Useful? Interesting? Leave me a comment