Skip to content
Advertisement

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

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="xxxxxx@example.com"]
// 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.

Advertisement

Answer

Here’s an example of a form tag I’ve used for getting US States. This is a <select> generated from an array. This is probably more along the lines of what you want to do.

You can see that I also use the usermeta billing_state to pre-select the choice.

With that said, you should also be able to use this same method to create a select tag that performs any WP_Query and puts the results into an option tag.

<?php

add_action('wpcf7_init', function (){
    wpcf7_add_form_tag( array('dd_states', 'dd_states*'), 'cf7_state_dropdown' , true );
});
function cf7_state_dropdown($tag) {

    $tag = new WPCF7_FormTag( $tag );

    $atts = array();

    $validation_error = wpcf7_get_validation_error( $tag->type );

    $class = wpcf7_form_controls_class( $tag->type );

    if ( $validation_error ) {
        $class .= ' wpcf7-not-valid';
    }

    $atts['class'] = $tag->get_class_option( $class );
    $atts['aria-required'] = 'true';
    $atts['aria-invalid'] = $validation_error ? 'true' : 'false';

    $atts = wpcf7_format_atts( $atts );

    // Get User ID and Billing State from DB
    $userid = get_current_user_id();
    $user_state = get_user_meta($userid, 'billing_state', true);

      $states = array ( 'FL'=>'Florida','AL'=>'Alabama','AK'=>'Alaska','AZ'=>'Arizona','AR'=>'Arkansas','CA'=>'California','CO'=>'Colorado','CT'=>'Connecticut','DE'=>'Delaware','DC'=>'District of Columbia','GA'=>'Georgia','HI'=>'Hawaii','ID'=>'Idaho','IL'=>'Illinois','IN'=>'Indiana','IA'=>'Iowa','KS'=>'Kansas','KY'=>'Kentucky','LA'=>'Louisiana','ME'=>'Maine','MD'=>'Maryland','MA'=>'Massachusetts','MI'=>'Michigan','MN'=>'Minnesota','MS'=>'Mississippi','MO'=>'Missouri','MT'=>'Montana','NE'=>'Nebraska','NV'=>'Nevada','NH'=>'New Hampshire','NJ'=>'New Jersey','NM'=>'New Mexico','NY'=>'New York','NC'=>'North Carolina','ND'=>'North Dakota','OH'=>'Ohio','OK'=>'Oklahoma','OR'=>'Oregon','PA'=>'Pennsylvania','RI'=>'Rhode Island','SC'=>'South Carolina','SD'=>'South Dakota','TN'=>'Tennessee','TX'=>'Texas','UT'=>'Utah','VT'=>'Vermont','VA'=>'Virginia','WA'=>'Washington','WV'=>'West Virginia','WI'=>'Wisconsin','WY'=>'Wyoming');

    $output = '<span class="wpcf7-form-control-wrap '.sanitize_html_class( $tag->name ).'"><select name="state" id="state" '.$atts.'>';
    $output .= "<option value=""> - - Choose State - - </option>";
    foreach ($states as $abbrev=>$state){
    $selected = ($user_state == $abbrev) ? ' selected="selected"' : '';
        $output .= '<option value="'.$abbrev.'"'. $selected .'>'.$state.'</option>';
    }
    $output .= "</select></span>";
    $output .= $validation_error;

    return $output;
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement