How to dynamically pass an array of values to a <select> field in Contact Form 7?

  contact-form-7, forms, javascript, php, wordpress

According to the official Contact Form 7 docs, it is possible to pass a a default value to CF7 from the shortcode, in this way:

// field in CF7 in WordPress admin area

[email* destination-email default:shortcode_attr]
// shortcode in a WordPress php template

[contact-form-7 id="123" title="Contact Form" destination-email="[email protected]"]
// function in functions.php

add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
 
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
  $my_attr = 'destination-email';
 
  if ( isset( $atts[$my_attr] ) ) {
    $out[$my_attr] = $atts[$my_attr];
  }
 
  return $out;
}

This works for a simple text field, but I need to pass an array of values to a <select> field and use them as <option>s inside it; I’ve tried to modify a bit this code, but apparently it isn’t working, or at least I’ve not been able to.

Is it possible to use the shortcode to send dynamic data to CF7 even if not a single plain text like this?

If not, I’m open to every other kind of solution, even if it involves another method, or some additional plugin; is there some other way to dynamically send an array of values to <select> fields in Contact Form 7?

These values are data queried from the database (such as post names, custom fields, and so on), so they need to come from php first even if there is a solution that involves javascript.

Source: Ask Javascript Questions

LEAVE A COMMENT