Stop WordPress adding <br/> tags

Been swearing at WordPress this afternoon…

I’ve been trying to transfer an html form, which I had working perfectly on a static html page, onto a WordPress page. WordPress insisted on adding multiple <br/> tags where I didn’t want them. Mutter.

Eventually, after much searching, I identified a parameter within wp-includes/formatting.php, which allows you to disable this irritating feature. If you open formatting.php and find the function wpautop you’ll see this:

function wpautop($pee, $br = 1)

Change this to:

function wpautop($pee, $br = 0)

Save the file and publish it back to your server and that’s it. Works fine in WP2.8.

Note: WP 2.8.3 overwrites formatting.php so this change needs redoing.

Update 24 October 2010

Fed up with repeatedly doing this I’ve developed a WordPress plugin to address the issue. Give it a try and let me know if it works for you.

Tags: ,

41 responses to “Stop WordPress adding <br/> tags”

  1. Lima says:

    cheers, mate!

  2. Great solution! It works perfectly! Thanks for sharing!

  3. I can now stop headbutting the desk – until the next update.

  4. Don says:

    Awesome. I too was doing some headbutting. $#%!!!

    Thanks a lot for posting this solution!

  5. Jason says:

    Thank you so much!

  6. Justin says:

    Awesome – this helped me out heaps. Thank you.

  7. Sierra says:

    You are a life-saver! I am forever indebted to you!

  8. Kris says:

    Great post – thanks so much.
    What an annoying little feature that is – auto adding line breaks behind your back!

  9. iamebet says:

    Thanks!!! I was stumped with this one when creating some shortcodes.

  10. Joyce says:

    Thanks a million, I was pulling my hair out!

  11. Ben says:

    Thanks! This was driving me crazy!!! And this fixed it 🙂

  12. wjzhb says:

    tx!It can solve the problem

  13. Graham says:

    You total and utter legend – its been driving me nuts

  14. Justin says:

    Definitely NOT the proper way to do this guys. If you ever update WordPress, you’ll need to edit this each time. Best option is to wrap the fix in a custom function.

  15. Simon says:

    …which is what I’ve said, and why I wrote the plugin…

  16. Tommy says:

    THANKS!!!!

  17. Thanks so much! been looking for that =) WordPress does a lot of strange things for which aparently there is no explanation. For example I can do things in the header and they work but if I do them in a static page of theirs it doesn’t. s have a weird extra 6px bottom margin in spite of there not being any in the CSS.

    What I mean to say is, thank you! =)

  18. Sean says:

    Thanks Simon, this was driving me batty!

  19. my1es says:

    I am in your debt, thank you!

  20. richie rich says:

    still. no luck.

    can u please tell me. Why its adding BR in DIVS and just before h tags. The problem is since i cutted thr tab content from HTML to WordPress as post/page.

    I’m calling the page using call getPageContent Function.
    can u tell why. This is the function

    http://www.tanzilo.com/2008/11/01/wordpress-get-page-content-by-page-id-static-pages-to-dynamic/

  21. Simon says:

    You have

    return nl2br($post->post_content);

    in your function.

  22. John says:

    Where is the wp-includes/formatting.php file?

  23. Simon says:

    Um…it’s in, well, wp-includes….but you might be better using the plugin anyway.

  24. Brad says:

    Exactly what I was looking for, thank youvery much!

    Cheer!

  25. Minley says:

    Superb solution,thanks a lot!

  26. Peter says:

    Just wanted to throw out a quick Thanks. Just what I needed. -P

  27. Arun says:

    Thank u very much. You are such a nice guy

  28. Arun says:

    How to disable auto p tag.

  29. Simon says:

    If you want to stop the formatting altogether then you can use the following:

    remove_filter('the_content','wpautop');

    Paste this into your theme’s functions.php.

  30. Frank says:

    Hi Guys,

    Am getting added and in my post where am using [APCAL] (appointment calendar plugin short-code).

    My theme details:
    Theme Name: Balance
    Author: Ignitethemes & Weblusive
    Version: 1.2

    I have tried above solution and all tags has been removed but tags are still there.

    Please help me to solve out this problem.

    Correcting my above post: br tags gone but p tags are still in post content.

  31. Simon says:

    Yes, the p tags will still be there because the code described just prevents the addition of br tags.
    See my response 29 above to disable all formatting.

  32. dani says:

    yeah, it works! thanks a lot

  33. ramonyemaya says:

    Thanks a lot!
    You are Great!

    In WP 3.8:
    function wpautop($pee, $br = false) {
    $pre_tags = array();

  34. PinaP says:

    Thanks so much be searching for a solution to this problem for a looong time.

  35. booksofhappiness says:

    This problem were making me mad, i used br {display:none;} with no success previously but the solution you’ve revealed here is the best answer. thanks a zillion

  36. mielie says:

    How do you stop WordPress adding tags before tables???

    Please can someone help me before I don’t have a head left. lol

  37. Simon says:

    I can’t recreate this – if I paste a table into WordPress it does not wrap any tags around it.

  38. denn15 says:

    You can just add 2 lines at the end of the functions.php file.

    http://iiiji.com/remove-automatic-and-tags-in-wordpress-2/

  39. Simon says:

    Yes, but that remove all the formatting entirely. This just stops the unnecessary line breaks.

Useful? Interesting? Leave me a comment

I've yet to find a way of allowing code snippets to be pasted into Wordpress comments - so if you're trying to do this you'd be better off using the contact form.