Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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) {

if(!isset($atts['style']))
    $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,
                );
            break;

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

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

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

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


    // do some stuff

}
share|improve this answer
    
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 '12 at 2:13
    
@Aras - glad to be of help :) –  Evan Mattson Jul 18 '12 at 2:58
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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