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 creating a Wordpress plugin. I want to create 1 single shortcode that changes output based on the settings changed by the user on the admin settings page.

So for starters, I have something like this below:

add_shortcode( 'my_shortcode', 'my_shortcode');
  function my_shortcode($atts) {
    $atts = shortcode_atts (
  array (
  'display' => get_option('setting_1'),
  'toolbar' => get_option('setting_2'),
  'branding' => get_option('setting_3')
  ), $atts);
     }

What do I need to do next in order to store these settings into a single shortcode that keeps it's name despite the settings changing?

share|improve this question
    
hat do you mean store options? Is this a question about shortcodes? Or how to create an admin page with options? –  chrisguitarguy Dec 7 '12 at 4:01
    
I need to take the settings chosen by the user on the setting page (already created) and then store them into a shortcode that displays HTML based on those chosen settings. I know this is a general question, so I apologize for not being more specific, I'm just confused. –  Rob Myrick Dec 7 '12 at 4:05
add comment

1 Answer

up vote 2 down vote accepted

If you want to alter display based on admin options, you don't need to deal at all with the shortcode_atts ar the $attributes that are passed into your callback. The attributes passed in (and set by shortcode_atts) are meant to alter the way a shortcode works based on how an end-user uses it.

Take this: [wpse75238 arg1="asdf" arg2="asdf"]

<?php
add_shortcode('wpse75238', 'wpse75238_shortcode_cb');
function wpse75238_shortcode_cb($atts)
{
    return var_export($atts);
}

Something like array ( 'arg1' => 'asdf', 'arg2' => 'asdf' ) will come out.

shortcode_atts just sets defaults for you (and removes "unauthorized" keys) in a similar way to wp_parse_args.

If you just want to change stuff based on an admin page option, you don't need to respect the user input.

<?php
add_shortcode('wpse75238', 'wpse75238_shortcode_cb');
function wpse75238_shortcode_cb()
{
    if('some_value' = get_option('wpse75238_opts'))
    {
        // do stuff because some_value was set.
    }
}

Calling the shortcode with arguments will not change a thing for the above shortcode.

share|improve this answer
    
thanks chris - I scrapped my function completely and applied your logic to another already-existing function and it worked. Thanks –  Rob Myrick Dec 7 '12 at 4:28
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.