Regular Expression (Regex) to Remove Empty WordPress Shortcode Parameters
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=””]
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"] ?>