Removing unwanted p and br tags from WordPress

When working with HTML, you sometimes don’t like your HTML editors because of the irritating automatic bugs. Yes, we are here with a solution to prevent/remove automatic <p> and <br> tags from word press editor. So, the first step you can do is to ensure the check-box Disable the visual editor when writing.

But sometimes this step will not work out. No worries! You should do it as early, as you can if you want to remove automatic <p> and <br> tags. Approximately all my handwritten <p> tags get removed after switching off automagically-mess-up-functionality of word press. Otherwise, you will have to clean up all your old posts then like I did. I have lost some hours with useless HTML editing at that time due to these irritating tags.

But surely you will not have to waste your time as I did. Because here is what you required, A SOLUTION!

So, follow the steps mentioned in the below lines and you will be in a comforting zone. Open the file wp-includes/default-filters.php of your WordPress installation and write out the following line:

addfilter('the_content', 'wpautop');

You can alternatively add the following to the functions .php file of your theme if you are developing your WordPress theme. Follow this line:

remove_filter('the_content', 'wpautop');

For instance, I doubted a bit, where all that whitespace were coming from in my posts. I usually wrote one sentence per line, or comment some empty lines in between to organise the code. Unfortunately, WordPress creates mess by automatically putting every sentence in its paragraph as it was commented on its line and putting <br> in between for portraying the empty lines.

But even worse, WordPress also shows unwanted <p> tags that break HTML code due to it. For instance, I particularly found out about this auto-mess-up functionality, because I was checking my post with an HTML-validator and was in doubt, why the validator was grumping about a <quote>-tag inside flow content.

In HTML, generally ‘Return/Enter’ will not line break your paragraph as you anticipated like in Microsoft Word. You have to put the code <br> to get the line break. Nevertheless, the line break is showed because of the default setting in WordPress for you by commenting ‘Return/Enter’ button on the keyboard irrespective of the <br> code. It is somewhat annoying because we are not used to it.

It is really easy to turn remove <br> & <p> tags on your WordPress. Here is another solution for you:

Go to Admin -> Appearance -> Editor -> function.php and copy & paste the following code below at very end.

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

It can be removed without using code. Follow:

Go to Settings > TINYMCE Advanced and check to Stop removing the <p> and <br> tags when saving and show them in the HTML editor.

It can also be removed with code. Simply follow:

Disabling unwanted <p> & <br> tags can be done by adding filter function to theme functions.php.

function remove_additional_p($content){
$content = forec_balance_tags($content);
return preg_replace('#<p>\s*+(<br\s*/*>)?|s*</p>#i','',$content);
}
	 
add_filter('the_content','remove_additional_p',20,1);

UNLIMITED DOWNLOADS: 50+ Million Website Templates & Design Assets

All the Website Templates you need and many other design elements, are available for a monthly subscription by subscribing to Envato Elements. The subscription costs $16.50 per month and gives you unlimited access to a massive and growing library of over 50 million items that can be downloaded as often as you need (stock photos too)!

Recent Articles

How To Speed Up Your WordPress

Optimizing the speed of your WordPress website is crucial for several reasons. Not only does a fast-loading website provide a better user...

How to Hide “Add to Cart” Button in WooCommerce Quickly

Once on your project, you will maybe need to disable selling products, make a catalog from your e-commerce store, or simply hide...

Agronix – Organic Farm Agriculture WordPress Theme [Free WordPress theme download]

Demo Download Agronix – Organic Farm WordPress Theme. We are Happy to presenting you the Agronix Agriculture Farming WordPress Theme. It is very easy to customize...

Roux – Creative Portfolio WordPress Theme [Free WordPress theme download]

Demo Download This is Roux, a splash of vibrant creativity your beautiful new website needs! Roux comes with tons of creative portfolio, gallery and graphic...

Get in touch

Be our first-class reader, build a community and follow us on social media!

Leave A Reply

Please enter your comment!
Please enter your name here