I am writing my first wordpress plugin which inserts a visualization widget inside a post. I am planning to use a shortcode for my plugin. I followed an excellent tutorial about writing shortcodes. But I still have one question in mind. I have many configuration parameters that will be associated with my widget for example size, color, background, type etc. I can think of two way to capture those configurations.

First is to use a simple shortcode such as this:

[my_widget id=1] 

And store the rest of the configurations as a record in the database. When I interpret the shortcode, I can make a lookup and retrieve the rest.

The second way is to pack all the configuration parameters inside the shortcode itself.

[my_widget id=1 color="red" size="600x600" type="one" etc etc etc.]

This would make a rather ironically long shortcode but it will not require a database lookup and will also enable users to edit parameters in place.

My question is, which of these two approaches do you recommend for creating shortcodes for plugins?

2 Answers 2


It sounds like your function that your shortcode is working with has a lot of parameters that it can take.

In general, I definitely think the better option is to use attributes for each parameter you want to control.

As you know from that tutorial, shortcodes can have default values defined for the variables and use the attributes from the shortcode to override them. This is what I would recommend doing.

You could even create a few different core "styles" to work from that have different default values to start with, and people can customize them how they like. This way, someone could start with a particular style, and then override parameters where necessary instead of requiring every single one to be defined by the shortcode itself.

Check this out:

add_shortcode('my_widget', 'my_widget_shortcode');
function my_widget_shortcode($atts) {

    $atts['style'] = ''; // if that attribute isn't set, it will throw an error in the switch otherwise

    // define defaults based on style
    switch ($atts['style']) {

        // style="1"
        case 1:
            $defaults = array(
                'color' => 'red',
                'height' => 600,
                'width' => 600,

        // style="dog"  
        case 'dog':
            $defaults = array(
                'color' => 'green',
                'height' => 400,
                'width' => 600,

        // style="frisbee"
        case 'frisbee':
            $defaults = array(
                'color' => 'blue',
                'height' => 200,
                'width' => 200,

        // no style attribute defined or something else that doesn't have a case defined above
        // eg: style="dinosaur"
            $defaults = array(
                'color' => 'yellow',
                'height' => 800,
                'width' => 500,

    // merge arrays and override defaults with shortcode atts
    extract(shortcode_atts($defaults, $atts));

    // do some stuff

  • Thank you for the comprehensive answer and example code. At this point I think I will go with the second option as you suggested.
    – Aras
    Jul 18, 2012 at 2:13
  • @Aras - glad to be of help :) Jul 18, 2012 at 2:58

The parameterized shortcode doesn't necessarily have to hit the database, and users can understand and customize it more easily. I think it wins overall.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.