Skip to content
Home » Documentation » Controller Fields for Contact Form 7 » Setting Options » Dynamic Options using Shortcodes

Dynamic Options using Shortcodes

You can use a shortcode to populate your options with three (3) important provisions:

  1. The shortcode should NOT include the normal square brackets ([ and ]). So, instead of [my_shortcode key='value'] you would use my_shortcode key='value'.
  2. Any parameters in the shortcode must use single quotes. That is: my_shortcode key='value' and not my_shortcode key="value"
  3. Shortcodes should return a string value of HTML of <option> and/or <optgroup> elements.

Note: your shortcode does not have to escape before returning (but it is recommended) because my plugin runs the output through the wp_kses() function before displaying on the page only allowing <optgroup> and <option> elements with the disabled and hidden attributes on both and additionally only value and selected attributes on the <option> elements and the label attribute on the <optgroup> elements.

Basic Example

Let’s say I want to create a dropdown of my WordPress plugins to choose from. I might create a shortcode in my active theme’s functions.php like this—it just has my services hardcoded as a string and returns it:

function au_get_plugins() {
    $o = '<option value="controllable-fields">Controllable Fields</option>';
    $o .= '<option value="accessible-reading">Accessible Reading</option>';
    $o .= '<option value="dtx">CF7 - Dynamic Text Extension</option>';
    return $o;
}
add_shortcode('get_plugins', 'au_get_plugins', 10, 0);

Then, I would enter my shortcode in the tag generator’s options textarea like this:

get_plugins

The tag generator will display it like this:

[select_controller plugin id:my-plugin "get_plugins"]

And on the frontend, you’ll see the options outputted as your shortcode

<select name="plugin" id="my-plugin" ...>
    <option value="controllable-fields">Controllable Fields</option>
    <option value="accessible-reading">Accessible Reading</option>
    <option value="dtx">CF7 - Dynamic Text Extension</option>
</select>

Advanced Example

Let’s say I want to create a dropdown of my WordPress plugins to choose from, but not just a list of my plugins, but also grouped by their category. I might create a shortcode in my active theme’s functions.php like this:

function au_get_plugins() {
    // TBA - Coming Soon
}
add_shortcode('get_plugins', 'au_get_plugins', 10, 0);

In this function, I’m pulling a list of all my products of the custom product post type. I then get its category taxonomy and create a multidimensional array. This will act as the basis of my option groups.

Finally, I loop through that multidimensional array to generate the HTML. I also want to pre-select the product if viewing that page and set it to readonly if that’s the case.

Then, I would enter my shortcode in the tag generator’s options textarea like this:

get_plugins

The tag generator will display it like this:

[select_controller plugin id:my-plugin "get_plugins"]

And on the frontend, you’ll see the options outputted as your shortcode

<select name="plugin" id="my-plugin" ...>
    <!-- TBA - Coming Soon -->
</select>

Related Post Module Attributes Before

array(29) {
  ["post_type"]=>
  string(4) "post"
  ["post_id"]=>
  string(1) "0"
  ["exclude"]=>
  string(1) "0"
  ["title"]=>
  string(32) "Related Articles & Tutorials"
  ["description"]=>
  string(0) ""
  ["max"]=>
  string(1) "3"
  ["post_ids"]=>
  string(0) ""
  ["exclude_ids"]=>
  string(0) ""
  ["is_series"]=>
  string(0) ""
  ["featured_term"]=>
  string(2) "72"
  ["exclude_terms"]=>
  string(0) ""
  ["exclusive"]=>
  string(1) "1"
  ["order"]=>
  string(4) "DESC"
  ["show_image"]=>
  string(2) "on"
  ["image_size"]=>
  string(6) "medium"
  ["menu_order_label"]=>
  string(0) ""
  ["show_order_label"]=>
  string(2) "on"
  ["show_date"]=>
  string(2) "on"
  ["show_meta_keys"]=>
  string(2) "on"
  ["show_modified"]=>
  string(0) ""
  ["show_author"]=>
  string(0) ""
  ["show_categories"]=>
  string(0) ""
  ["show_primary_category"]=>
  string(0) ""
  ["show_description"]=>
  string(0) ""
  ["show_reading_time"]=>
  string(2) "on"
  ["show_cta"]=>
  string(2) "on"
  ["cta"]=>
  string(9) "Read more"
  ["autoplay"]=>
  string(0) ""
  ["allow_sticky"]=>
  string(0) ""
}

Related Post Module Attributes

array(29) {
  ["post_type"]=>
  string(4) "post"
  ["post_id"]=>
  string(1) "0"
  ["exclude"]=>
  string(1) "0"
  ["title"]=>
  string(32) "Related Articles & Tutorials"
  ["description"]=>
  string(0) ""
  ["max"]=>
  string(1) "3"
  ["post_ids"]=>
  string(0) ""
  ["exclude_ids"]=>
  string(0) ""
  ["is_series"]=>
  string(0) ""
  ["featured_term"]=>
  string(2) "72"
  ["exclude_terms"]=>
  string(0) ""
  ["exclusive"]=>
  string(1) "1"
  ["order"]=>
  string(4) "DESC"
  ["show_image"]=>
  string(2) "on"
  ["image_size"]=>
  string(6) "medium"
  ["menu_order_label"]=>
  string(0) ""
  ["show_order_label"]=>
  string(2) "on"
  ["show_date"]=>
  string(2) "on"
  ["show_meta_keys"]=>
  string(2) "on"
  ["show_modified"]=>
  string(0) ""
  ["show_author"]=>
  string(0) ""
  ["show_categories"]=>
  string(0) ""
  ["show_primary_category"]=>
  string(0) ""
  ["show_description"]=>
  string(0) ""
  ["show_reading_time"]=>
  string(2) "on"
  ["show_cta"]=>
  string(2) "on"
  ["cta"]=>
  string(9) "Read more"
  ["autoplay"]=>
  string(0) ""
  ["allow_sticky"]=>
  string(0) ""
}