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

Recent Articles

How to bulk delete WordPress comments in PHPMyAdmin

Any user working on the computer system has the option in quickly deleting all the WordPress comments by using MySQL or phpMyAdmin....

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...

The 7 Best WordPress Plugins For Google Analytics 2020

As the saying goes, "what is not measured, is not managed." After setting up a WordPress website, it’s important to see what’s...

How to hide all WordPress update notifications in dashboard

When working on a project, it can be frustrating to have to deal with all the update notifications that are in the...

How to add custom HTML to wp_nav_menu

WordPress has a lot of features that help us solve almost all daily tasks of any programmer or web developer. Due to...

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