• 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 modifiedDecember 7, 2022

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

That’s frustrating. You need to remove those empty p tags to prevent your content from being distorted. 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

Hello! 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

Comments

  1. Matt

    October 6, 2022

    This worked for me! Thanks for posting this trick!

    Reply
    • Thomas Griffin

      October 10, 2022

      You bet, Matt! Glad it removed those empty paragraph tags for you. 🤩

      Reply

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 to Thomas Griffin 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.

Most people would rather live a lifetime of self-deception than deal with the pain in moments of self-reflection.

Thomas Griffin

Footer

ThomasGriffin.com Logo

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

Connect

Sites I Like
OptinMonsterTrustPulseWPFormsMonsterInsightsWPBeginnerEclectic TigerSyed BalkhiAwesome MotiveT214

Misc
WordPressPrivacyDisclosures Sitemap

The Better Leader Newsletter

The Better Leader Newsletter

Join 6,889+ leaders taking the right steps to grow in their leadership. No spam - just real, thought-provoking stories and advice to make you a better leader. Grow in your leadership by subscribing today!