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: Amended 31 March 2014
<?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: 2.0
Author: Simon Battersby
Author URI: http://www.simonbattersby.com
*/
function better_wpautop($pee){
return wpautop($pee,false);
}
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'better_wpautop' , 99);
add_filter( 'the_content', 'shortcode_unautop',100 );
?>
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. If you’re downloading this after 31 March 2014, 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
working great on 2022! awesome job!
Had 5,000+ html files to import and WordPress was putting br right the way through, driving me crackers. This plugin still works a treat, total lifesaver.
Still Working great with 5.1.1, Thank you Simon
Line 429 of formatting.php is:
That’s what you want.
Hi Simon,
I can’t find this code in formatting.php. I saw this code : function wpautop($pee, $br true
What can i do? Please help me
Mike
I’m not sure why it doesn’t work for you. It’s possible that your theme or another plugin is interfering – although that would be unusual. If you’re able, change to a vanilla theme and/or disable your other plugins.
Hi,
It doesn’t work for me.. WP still keeps adding the ..
Mind Blowing solution Thank you…!!!
Depends how you are parsing the text entered on the form before storing it in your database. You might want to look at the PHP function nl2br or google the often used local function nl2p.
I create a form to insert post into wp.
if the user input several lines, why it becomes one line in wordpress?
line 1
line 2
becomes
line 1 line 2
Works like a dream! Thanks man!
Thank you for this plugin.
I’m a WP beginner, but have had some basic experience with other CMS and HTML / CSS. For half a day, I had been inspecting my css which worked on a custom html page perfectly but became a scary mess when inserted into a WP “page”, apparently because of the line break tags. Thank you very much for the solution. Worked with 4.2.3, did the upgrade to 4.2.4, still working! 🙂
Didn’t realise it wasn’t on this page! It is now….
BLESS YOU!
Do you have a donate button lying around? I’d love to buy you a beer.
Running on WordPress 4.1
Thanks so much!!
Oh my goodness. Thank you so much. I have never run into this problem until today and your plugin fixed the issue right away. I am using WordPress 4.1 and it is working beautifully. 🙂
Thank you so much! How maddening this problem was. My H tags had an annoying break before them using a column shortcode. This has solved it for me without destroying hard returns. Thank you so much! (BTW working fine in 4.0.1
Thanks for this. Plugin working fine in WordPress version 4.0.1
The first thing would be to check the html generated by the widget, and see if that’s causing the issue. The plugin solves the problem created by WordPress built in filter which runs on post content, so a widget will not be affected by this anyway.
Hi Simon! Thanks for getting back here. Is there any solution for widgets in the sidebar that have forms in them? You rock. Thanks!
There’s no way of knowing what’s happening from a screenshot, but my plugin will work only on post content. If your payment plugin is running the form as a widget (for example) then the
tags are possibly being added directly, in which case you’d need to look at the html that your plugin generates.
Hi Simon,
I’m using your plugin (to no avail yet). The issue I’m having is I’m using the WordPress PayPal Payment Plugin on a Genesis theme using a sidebar widget. It’s still adding an extra tag as you can see here: http://prntscr.com/55jzqy
Maybe I’m doing something wrong?
Works fine here – thanks for taking the time to do this. ( October 2014 WP4)
Magic! Worked for me with tables (October 2014 WP 4.0)
Simon, the plugin is exceptional for WP beginners like me, thank you very much for sharing!
Why does this happen?
I stumbled on your plugin searching for what was causing 20 tags to be inserted in a Paypal form sitting in a table cell. I installed it as a last resort before beginning days of forum posting…
Can’t thank you enough.
Heh, you’re right, I get the same result. This is a combination of the formatting applied through the WordPress wpautop function and the html it generates for a gallery, and isn’t fixable with this plugin. The issue I think is that the standard wp gallery doesn’t allow you to set ‘none’ for the number of columns in a gallery to enable a responsive design. This can be addressed, sort of, by
.gallery br {display: none;}
in your css.The plugin works great!!! Thanks a lot, it save me hours
I tried using this with WP 3.9.1 to remove break tags from galleries and after installing WordPress added paragraph tags around the break tags instead of removing anything. Thanks for your hard work on this frustrating issue: hope my post helps in development.
Installed on May 13, 2014. Worked right away. Thanks very much.
Thank you, these are valid points, and I have amended the plugin which I believe addresses your issue. Shame you had to make the points in such a manner, and anonymously…
Well, the problem is, this solution changes the order of the assigned filters. wpautop now runs after shortcode_unautop and other replacements, which is quite senseless.
So the better way, the hacky way, could be to assign your callback directly to the filter variable like this:
$wp_filter[‘the_content’][10][‘wpautop’][‘function’] = ‘better_wpautop’;
Additionally, the way you call the funktion is wrong. There’s no reason to use $br here – The call only uses the value you send. In short: use false or 0.
I’m a web designer and this is a wonderfully helpful plugin. Thank you so much:)
Hi Wayne
The plugin works fine for me on 3.8.1 without the change. PHP will interpret 1 as true and 0 as false anyway.
In the latest version of wordpress 3.8.1 it does not use 0 or 1 anymore. You need to edit function wpautop($pee, $br = true) and change it to:
wpautop($pee, $br = false)
As far as I can remember there was a bug some while back which prevented uploading images – maybe that’s what you’re seeing. Can you not upload images via Media and then link to them instead?
Thank you! Ended an hour or so of frustration
It took me so long to figure out that it was wpautop messing up my formatting. Thank you, very useful.
The code given is the whole code for a plugin. If you’re pasting into functions.php then you need to remove the starting and ending php tags and paste the code below before the existing closing php tag in your functions php (?>):
Let me try this again without the code snippet… when i take your code exactly as it is and paste it at the end of the functions.php file. I get an error saying “an unexpected < in the code on line 258". I am not a php expert, so i believe i am just missing a little something… any help?
Lifesaver! Thanks for making the plugin.
Tim
It’s possible the tags are coming from MailChimp itself rather than WordPress. Another last ditch approach if you can’t edit the MailChimp html is to set the
<br/>
tags to display:none via your css. This won’t work of course if the MailChimp form is in an iframe.Not working for me.
I am having the same issues as you. Adding a form (MailChimp) inside a page
Awesome – I just wished I’d noticed those extra tags in the page source code (of a form) instead of chasing non-existing conflicting stylesheets for three hours! Thanks so much.
Thanks for this function. Save me a lot of headache! 😉
YAY! Thank you! Works great!
Brilliant! Thank you so much for this quick fix.
Thanks – also put this in functions.php and works great
I would send you 20 virgins if I could. This plugin saved me from blowing my brains out. Thank you!
Finally!!! Thanks!
Thanks, you just ended 20 minutes of aggravation.
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.
This works great! 3.3.1. WP added 10 br tags to every page I created.
It works fine for me in 3.3.1 – previous commenter Sophie also got it working OK.
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? ðŸ™
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.
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
Yes, just tested it and it works fine.
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…
Thanks a lot for this! Just learning about WordPress and this was really pissing me off as well!!
It worked! I think this solves a big issue for me. Thank you!
Worked wonders for me, thanks for posting this solution!
– WordPress 3.2.1
AWESOME!! Fixed it for me too, I added the code to functions.php and it works perfectly! Thank you 🙂 – WP3.2.1
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.
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?
Thanks! This was driving me crazy! I’m running WP 3.1.4
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)
Thanks for this. I put the code in my functions.php file and it work perfectly. I appreciate it.