• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Thomas Griffin

Be a better leader

  • About
  • Newsletter
  • Articles
  • Business
  • Favorites
  • Contact

How to Remove Empty Paragraph Tags from Shortcodes in WordPress

Thomas Griffin Avatar
written byThomas Griffin
last modifiedJune 11, 2021

Sometimes when you add WordPress shortcodes in your content, empty paragraph tags are added too!

That’s frustrating. I had it happen enough that I decided to do something about it. I created a quick code snippet to remove those empty paragraph tags from shortcodes on my own WordPress site, and I thought it would be nice to share it with you as well!

Simply drop the following code in your theme’s functions.php file:

How to Remove Empty Paragraph Tags from Shortcodes in WordPress
/** 
 * Removes empty paragraph tags from shortcodes in WordPress.
 */ 
function tg_remove_empty_paragraph_tags_from_shortcodes_wordpress( $content ) {
    $toFix = array( 
    	'<p>['    => '[', 
    	']</p>'   => ']', 
    	']<br />' => ']' 
    ); 
    return strtr( $content, $toFix );
}
add_filter( 'the_content', 'tg_remove_empty_paragraph_tags_from_shortcodes_wordpress' );

That’s it! All empty paragraph tags have been removed from your shortcodes.

If you’ve found this content helpful, please share it with others! You can do so by clicking on one of the social sharing buttons in this article.

FacebookTweetLinkedIn
Thomas Griffin Avatar
Thomas Griffin

Hey friend! I'm Thomas, and I've been in WordPress for 10+ years. I've contributed to core and created the TGM Plugin Activation class. My WordPress plugins are active on over 19,000,000 websites and include popular ones like OptinMonster, WPForms and MonsterInsights.

View my WordPress toolbox•More about me →

Reader Interactions

Trackbacks

  1. ショートコードの使い方 [WordPress] – Site-Builder.wiki says:
    April 3, 2022 at 10:18 am

    […] How to Remove Empty Paragraph Tags from Shortcodes in WordPress | Thomas Griffin […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

previous article: How to Leverage Browser Caching in WordPress via .htaccess
next article: How to Include Custom Post Types in WordPress Archive Pages
Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.

The way to get started is quit talking and begin doing.

Walt Disney

Footer

ThomasGriffin.com Logo

Copyright © 2010-2022 Griffin Media LLC.
All rights reserved.

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome Motive

Misc
Don't Click HereWordPressPrivacyDisclosures Sitemap

Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader

Breakthrough Leadership

Great leaders share common traits that separate them from the pack. Download your copy of my signature ebook, Breakthrough Leadership: 5 Surprisingly Simple Qualities of Today's Exceptional Leader, absolutely FREE when you enter your name and email address below.