shortcode to create dynamic dropdown box form shortcode attributes

Based on your example at the bottom of your question, and your statement that you would like something like that for readability, I’d do something like this:

function dropdown_option($atts) {
  $dropid = (isset($atts['dropid'])) ? $atts['dropid'] : '';
  global $sco_array;
  if (!empty($atts['value']) && !empty($atts['text'])) {
    $sco_array[$dropid][$atts['value']] = $atts['text'];
  }
}
add_shortcode('sco','dropdown_option');

function sc_dropdown($atts) {
  $id = (isset($atts['id'])) ? $atts['id'] : 'sc_dropdown';
  $dropid = (isset($atts['dropid'])) ? $atts['dropid'] : '';
  global $sco_array;
  $sel="";
  if (!empty($sco_array[$dropid])) {
    $sel .= '<select id="'.$id.'" >';
      $sel .= '<option value="option0">Please select a module to download</option>';
      foreach($sco_array[$dropid] as $k=>$v) {
        $sel .= '<option value="'.$k.'">'.$v.'</option>';
      }
    $sel .= '</select>';
  }
  return $sel;
}
add_shortcode('scd','sc_dropdown');

Then you can build and display your dropdown with two shortcodes:

[sco dropid="one" value="http://example.com" text="Option1"]
[sco dropid="one" value="http://example1.com" text="Option2"]
[sco dropid="one" value="http://example2.com" text="Option3"]
[scd dropid="one" id="wlmm-select-dropdown"]

The first shortcode builds your options one at a time– three shown. The second one displays it. The dropid value should let you put multiple selects on the same page, if you wanted to.

Leave a Comment