• qbaas

    (@qbaas)


    When I populate select fields dynamicly using the “wpcf7_form_tag_data_option” filter and the “data:” attribute, I find that pipes that exist in the strings of the ingested array, are displayed literally, and are not interpreted, i.e.: The ‘before’ and ‘after’ value as well as the pipe itself, gets displayed in the contact form.

    Is there a way to combine pipes with dynamic select fields?
    Both are great options!!!

    Thanks a lot in advance!

    Best regards, Rene.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Erik

    (@codekraft)

    can you post your code? I think you just need to fill the value field (in addition to $tag->name)

    Thread Starter qbaas

    (@qbaas)

    Thanks for asking!

    In the contact form, I have:

    [select* your-coach class:cf7-field data:vccoach placeholder “Coach”]</label>

    In functions.php, this is the corresponding part:

    add_filter('wpcf7_form_tag_data_option', function($n, $options, $args) {
      if (in_array('vccoach', $options)){
        $coaches = getCoaches();
        return $coaches;
      }
      return $n;
    }, 10, 3);

    getCoaches() is a method that gets data from an external API endpoint.

    In that method I construct a new array from the JSON output by the external API endpoint, consisting of ID’s and Names. In a foreach loop through the response, this code:

    array_push($coaches, $coach['Id'] . '|' . $coach['Name']);

    … builds an array that consists of entries like :
    [“024|John Doe”,”026|Joan Does”, …]

    But the result of this, is 024|John Doe and 062|Joan Does … as options in the contact form select field, so the Pipe character isn’t interpreted like it is in strings literals.

    What am I missing? Thanks again for any help!

    Erik

    (@codekraft)

    isn’t easy without seeing the final array defined by getCoaches() but however check how listo does this in contact-form-7/modules/listo.php because it is exactly what you are looking for:

    foreach ( (array) $options as $option ) {
    		$option = explode( '.', $option );
    		$type = $option[0];
    		$args['group'] = isset( $option[1] ) ? $option[1] : null;
    
    		if ( $list = listo( $type, $args ) ) {
    			$data = array_merge( (array) $data, $list );
    		}
    	}
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Dynamic Select fields and pipes’ is closed to new replies.