One of the things that initially made WordPress into the hugely popular CMS that it is today was its ease of use.
But some of the conveniences that WordPress comes with is not always desirable, like the fact that WordPress automatically adds <p> (paragraph) tags to content.
Here on Templ.io for example, we don’t use any page builder or WYSIWYG to construct our pages. Instead, all of our pages are made up of very precise and manually written HTML markup, and in our case it was a pain in the *** when WP automatically added <p> all over our page content, which sometimes screwed up our page designs completely.
Luckily, we managed to disable this functionality by adding a small code snippet to our theme’s functions.php file.
We wanted to keep the automatically added <p> tags in our blog posts, but remove them from our pages. Here is the code that we use:
// Prevent WP from adding <p> tags on pages function disable_wp_auto_p( $content ) { if ( is_singular( 'page' ) ) { remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' ); } return $content; } add_filter( 'the_content', 'disable_wp_auto_p', 0 );
If you instead wish to prevent WordPress from adding <p> tags on both pages, posts and all other post types, you can simply remove the if ( is_singular( 'page' ) )
statement, like this:
// Prevent WP from adding <p> tags on all post types function disable_wp_auto_p( $content ) { remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' ); return $content; } add_filter( 'the_content', 'disable_wp_auto_p', 0 );
If you want to prevent WP from adding <p> tags to any other post type, you can simply replace page in if ( is_singular( 'page' ) )
with your post type of choice.
Smart, huh?
Have you found any other smart way to apply this code, or if you have some other neat little WordPress tweak to share? Please feel free to leave a comment below.
Hello, I used your fix on my theme, but other then paragraph it also remove br. There’s a way to prevent this?
Hi,
Sorry, but I don’t know of any way to achieve that. If you find one, I would appreciate if you post it here so that I can update the post accordingly.
Cheers,
Emanuel
The below jQuery script will remove all elements under #myDiv. Check the code Below:
$(document).ready(function () {
$(‘#myDiv’).find(‘br’).each(function(index){
$(this).remove();
});
});
Thank you very much
Good article bro, I’m so happy I found blog like yours. Really appreciate work you do for us, already applied sugesstion on my site.
cheers for the aweosme post as always
Hi, what code would I use to; keep the automatically added tags in our blog posts and pages, but remove them from the post excerpts? Thanks.
Hello there. You created a function. But you did not mentioned that now you need to call it for it to work.
Thanks for this quick tutorial. Sometimes the tags becoming really annoying, so I will prevent tags.
This doesn’t work. I’ve got 10 years of dev experience so I’m sure the error isn’t on my side. I copypasted this into my functions.php, and it didn’t work. I’m working off of a clean template, so it couldn’t have been the template overriding anything either.
Awesome! Thanks for that tip.
Hi, thanks for this insightful tip.
I am not that experienced, and I got it working but assume it’s for the whole site.
Can I change the code to do it for one page only?
Thanks!
Nice Article.
Thanks
WordPress always suprise me, your post saved me tho, big thanks!