Search code examples
phpwordpressshortcodecontact-form-7

Wordpress CF7 Getting all values of the Drop-down from shortcode attributes


I manage to get the default value for the drop-down in cf7 but I wonder is it possible to get all the values from the shortcode.

It would be helpful to have them with pipes since I need to pass an ID and label to it.

If I’m not clear. I am trying to build the drop-down on this form:

[select your-recipient "CEO|55"
                    "Sales|45"
                    "Support|99"]

but those three values I need to pass via shortcode.

Reference: https://contactform7.com/getting-default-values-from-shortcode-attributes/


Solution

  • I resolve that by creating a dynamic select field within functions.php. The select field values I am populating using a public variable.

    function dynamic_field_values($tag, $unused)
    {
        $dates = get_query_var('DATES');
    
        if ($tag['name'] != 'dates')
            return $tag;
    
    
        if (!count($dates ) > 0) {
            $tag['raw_values'][] = 'No dates available';
            $tag['values'][] = 'No dates available';
            $tag['labels'][] = 'No dates available';
    
            return $tag;
        } else {
            foreach ($dates as $date) {
                $tag['raw_values'][] = $date->id;
                $tag['values'][] = $date->id;
                $tag['labels'][] = $date->caption;
            }
    
            return $tag;
        }
    }
    add_filter('wpcf7_form_tag', 'dynamic_field_values', 10, 2);