How To Remove Unwanted P Tags from WordPress Posts & Widgets?
Today I have added few lines of html code in HTML / Text widget of WordPress and I was constantly getting a paragraph / line space <p></p> added automatically to my widgets. It was very annoying and I had tried various plugins to disable / remove unwanted paragraph tags from my wordpress site’s widget. None of them worked for me. They might work fine for wordpress posts, but not for the widgets.
Remove Unwanted Paragraph Tags from WordPress Widgets
So after researching for almost 30 minutes I had found a simple piece of code, that I had added to functions.php file of my wordpress theme, saved it and viola it simply removed unwanted paragraph tags / spacing from my text widget.
add_filter( 'widget_display_callback', 'wpoim_widget_display_callback', 10, 3 ); function wpoim_widget_display_callback( $instance, $widget, $args ) { $instance['filter'] = false; return $instance; }
Remove Unwanted Paragraph Tags from WordPress Posts / Shortcodes / Pages / Content Areas
Copy / paste the following piece of code to your theme’s functions.php file and you will remove all auto generated unwanted paragraph tags <p></p> from your wordpress posts / pages and content areas.
add_filter('the_content', 'remove_empty_p', 20, 1); function remove_empty_p($content){ $content = force_balance_tags($content); return preg_replace('# \s*+(<br\s*/*>)?\s* #i', '', $content); }
Or you can try out the following plugins to remove unwanted auto p tags.