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);

Recent Articles

Why is WordPress Asking For Your FTP Credentials to Install Plugins?

You want to install any plugin, but *blink* - WordPress need your FTP credentials. What the hell? The problem is simple -...

Fix PHP Warning: POST Content-Length of 8978294 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

This error is getting probably when you trying to upload an import on WordPress on your XAMPP local dev environment, right? Well,...

How to Hide Shipping Methods in WooCommerce?

This is a small detail, but WooCommerce doesn't have built-in any function for hiding shipping methods based on different situations. Imagine when...

How to Get WordPress Post Featured Image URL

Are you a WordPress developer or an advanced WordPress user? Whoever you are, sometimes you may need the direct URL of a...

How to Bulk Delete Media in WordPress

WordPress and media. That's the topic for a book. Sometimes is not easy to find what you need and when you have...

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