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?

  • 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

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"]

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.

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.

| 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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