Regular Expression (Regex) to Remove Empty WordPress Shortcode Parameters

Post written by David Wells
On Thursday, October 17th, 2013

This is a handy regular expression snippet to find and remove unused shortcode parameters from a wordpress shortcode.

It will grab all empty shortcode params like parameter=”” inside a shortcode bracket and remove them.

We used this in our new form tool coming out soon to clean up the markup seen by users in the wordpress editor before inserting a shortcode into the wordpress editor.

Regex Pattern:

/[a-zA-Z0-9_]*=""/g

Matches:

[inbound_field label=”label 1″ type=”text” description=”” required=”0″ dropdown=”” radio=”” placeholder=”” html=”” dynamic=””] regex

Output from find/replace:

[inbound_field label="label 1" type="text" required="0"]

PHP Regex Remove Empty Shortcodes Function:

<?php
$shortcode = '[inbound_field label="label 1" type="text" description="" required="0" dropdown="" radio="" placeholder="" html="" dynamic=""]';
$regex_pattern = '/[a-zA-Z0-9_]*=""/g';
$replacement = '';
echo preg_replace($regex_pattern, $replacement, $shortcode);
// output = [inbound_field label="label 1" type="text" required="0"]
?>

About the Author:
David is Founder of Inbound Now and a Fanatical WordPress Designer & Developer. He believes that the internet is a magical place where wonderful things can happen. Say hi to him @DavidWells

Posted in: Code WordPress
Signup and get started with Inbound Now

Sign Up for Free

Download

[gravityform id="3" name="Popup Form" title="false" description="false" ajax="true"]