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 Change WordPress Theme If It Does Not Work (No Access to wp-admin) Through phpMyAdmin

Something is wrong. Your WordPress theme has got a blank screen and you can not log in to your admin through /wp-admin/...

How to Bulk Deactivate WordPress Plugins (No Access to WP-Admin) Through phpMyAdmin

Sometimes happens that you don't have any backups of your WordPress site (ups) and after your plugins update nothing works (ups, twice!),...

Landco – Garden & Landscaping WordPress Theme [Free WordPress theme download]

Demo Download Landco – Garden & Landscaping WordPress Theme Landco – Garden & Landscaping...

Optrica – Optometrist & Eye Care WordPress Theme [Free WordPress theme download]

Demo Download Optrica – A Premium WordPress Theme for all kind of optometrist...

Qwery – Multi-Purpose Business WordPress Theme + RTL [Free WordPress theme download]

Demo Download Giant Multi-Purpose Business Elementor WordPress Theme v1.0.4 Qwery is an innovative responsive...

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