Skip to content

PHP WordPress Reform Shortcode String Inside ‘do_shortcode’

Here is how you can reform the original shortcode string solely based on the $attributes parameter in your do_shortcode function. This is useful if you want to show users examples of your shortcode, without your shortcode, well.. getting converted into actual shortcode content.

if( array_key_exists("reform_shortcode", $attributes) ) {
    $shortcode_string = "[YOUR_SHORTCODE_NAME ";
    foreach($attributes as $attribute_key => $attribute_value) {
        if( is_numeric($attribute_key) ) {
            $shortcode_string .= " {$attribute_value} ";
        } else if( is_string($attribute_key) ) {
            if( strcmp($attribute_key, 'reform_shortcode') !== 0 ) {
                $shortcode_string .= "{$attribute_key}='{$attribute_value}' ";
            }				
        }
    }
    $shortcode_string .= "]";
    return $shortcode_string;
}

In the second line, replace YOUR_SHORTCODE_NAME with whatever your shortcode name is. To use it, simply add a reform_shortcode=''attribute in your shortcode, like from

[shortcode url='http://abc.com/mp4']

to

[shortcode url='http://abc.com/mp4' reform_shortcode='']

Enjoyed the content ? Share it with your friends !
Published inDevelopmentProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.