skip to Main Content

I need to send a full array of custom field to a mail (dynamicaly populate) with contact Form 7 to work it here before sending :

// define the wpcf7_posted_data callback
function action_wpcf7_posted_data($array)
{
    $a = get_field('date')
    //WORK HERE

    $array['Nom & Prénom'] = $array['name'];
    unset($array['name']);

    $array['E-mail'] = $array['email'];
    unset($array['email']);

    $array['Téléphone'] = $array['tel'];
    unset($array['tel']);

    $array['Profession'] = $array['job'];
    unset($array['job']);

    $array['Session'] = $array['upcoming-gigs'];
    unset($array['upcoming-gigs']);

    unset($array['privacy']);

    return $array;
}

add_filter('wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1);

Because it’s before sending a mail I can’t call anything to compare before sending.

So I want to send all the data in a hidden input next to compare it.

This the two input in contact Form 7 :

      [select upcoming-gigs data:gigs id:date] [hidden select upcoming-gigs2 data:gigs2]

My goal here is to send all the data of the hidden select.

I don’t find a way to send all input in the mail.

Is it possible ? There is a better way ?

Thx

EDIT :

My question mark2 :

The goal is to send a mail with the date of the session and the id of it.

I use ACF and I have :

enter image description here

And after a dynamic dropdown, it’s look like this for the user :

enter image description here

The problem is I don’t have the id of the session, only the date.

To know the id I need to compar to the array of all the custom field, I can’t import it during wpcf7_posted_data.

I think if I send all the data of the array in a hidden field, I could remake the array and find the id of the session my user choose.

I hope I’m clearer.

(I can’t make a request in php during wpcf7_posted_data. Can I make an ajax request ?)

EDIT2 :
This my hidden select with session and text
enter image description here

This is the html of contact form 7 the rest is div for CSS
[select upcoming-date data:date id:date] [hidden select upcoming-date2 data:date2]

EDIT3 :
Okay get it.

The custom fields I use to make the dropdown are in two part id and text. I have the text part I need the id.

If I send every text and id in the mail I can compare to the answer of the user et add to the mail the right id.

Here the generated html : http://www.sharemycode.fr/5ax

EDIT 4 :
That where I write the id and text of the dropdown :

enter image description here

That where I create the select :

add_filter('wpcf7_form_tag_data_option', function ($n, $options, $args) {
    $ses = (array)get_field('date_new');
    $sesCount = count($ses);
    $gigs = [];
    $gigs2 = [];

    if (in_array('gigs', $options)) {
        for ($i = 0; $i < $sesCount; $i++) {
            if ($ses[$i]['date_start'] > date('d-m-Y', time())) {
                $a = "A réaliser entre le " . $ses[$i]['date_start'] . " et le " . $ses[$i]['date_end'] ." | bla";
                array_push($gigs, $a);
            }
        }
        return $gigs;
    }
}

2

Answers


  1. It looks like this is supported by Contact Form 7 natively, it’s just not very obvious on how to make it happen.

    Here’s a documentation page explaining the functionality: http://contactform7.com/selectable-recipient-with-pipes/

    Basically all you have to do is put the values like so:

    Visible Value|actual-form-value

    What comes before the pipe "|" character will be shown in the form, and what comes after will be the actual value filled in for the form.

    EDIT kanarpp :
    I add my code here to separate the answer of HowardE.

    This is how I dynamicaly create my select :

       add_filter('wpcf7_form_tag_data_option', function ($n, $options, $args) {
        $ses = (array)get_field('date');
        $sesCount = count($ses);
        $date= [];
    
        if (in_array('date', $options)) {
            for ($i = 0; $i < $sesCount; $i++) {
                if ($ses[$i]['date_start'] > date('d-m-Y', time())) {
                    $a = "A réaliser entre le " . $ses[$i]['date_start'] . " et le " . $ses[$i]['date_end'] ." | bla";
                    array_push($date, $a);
                }
            }
            return $date;
        }
    

    It’s not working, I use Smart Grid-Layout Design for Contact Form 7 to create dynmicaly my select

    Login or Signup to reply.
  2. I would create a custom form tag for the select. The following code will create a custom form tag called [gigs] which would be used like this:

    [gigs upcoming-gigs]

    I’ve also included ability to add the * and make it required.

    My assumptions are how you’re actually getting the ACF fields, which I can’t actually do, since I don’t have them, and you haven’t completely shared how it’s stored. You would add this to your functions.php.

    add_action('wpcf7_init', function (){
        wpcf7_add_form_tag( array('gigs', 'gigs*'), 'dd_cf7_upcoming_gigs' , array('name-attr' => true) );
    });
    function dd_cf7_upcoming_gigs($tag) {
    
        if ( empty( $tag->name ) ) {
            return '';
        }
    
        $validation_error = wpcf7_get_validation_error( $tag->name );
    
        $class = wpcf7_form_controls_class( $tag->type );
    
    
        if ( $validation_error ) {
            $class .= ' wpcf7-not-valid';
        }
    
        $atts = array();
    
        $atts['class'] = $tag->get_class_option( $class );
        $atts['id'] = $tag->get_id_option();
        $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
    
        if ( $tag->is_required() ) {
            $atts['aria-required'] = 'true';
        }
    
        if ( $validation_error ) {
            $atts['aria-invalid'] = 'true';
            $atts['aria-describedby'] = wpcf7_get_validation_error_reference(
                $tag->name
            );
        } else {
            $atts['aria-invalid'] = 'false';
        }
        // Make first option unselected and please choose
        $html = '<option value="">- - '. __('Please Choose', 'text-domain'). ' - -</option>';
    
        // This part you may have to update with your custom fields
        $ses = (array)get_field('date_new');
        $sesCount = count($ses);
    
        for ($i = 0; $i < $sesCount; $i++) {
            if ($ses[$i]['date_start'] > date('d-m-Y', time())) {
                $a = "A réaliser entre le " . $ses[$i]['date_start'] . " et le " . $ses[$i]['date_end'];
                // if session ID is in fact $ses[$i]['session']
                $html .= sprintf( '<option %1$s>%2$s</option>',
                    $ses[$i]['session'], $a );
            }
        }
        
        foreach ($gigs as $key => $value){
            $html .= sprintf( '<option %1$s>%2$s</option>',
                $key, $value );
        }
    
        $atts['name'] = $tag->name;
    
        $atts = wpcf7_format_atts( $atts );
    
        $html = sprintf(
            '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
            sanitize_html_class( $tag->name ), $atts, $html, $validation_error
        );
    
        return $html;
    }
    add_filter( 'wpcf7_validate_gigs', 'dd_validate_gigs_filter', 10, 2 );
    add_filter( 'wpcf7_validate_gigs*', 'dd_validate_gigs_filter', 10, 2 );
    
    function dd_validate_gigs_filter( $result, $tag ) {
        $name = $tag->name;
    
        $has_value = isset( $_POST[$name] ) && '' !== $_POST[$name];
    
        if ( $has_value and $tag->has_option( 'multiple' ) ) {
            $vals = array_filter( (array) $_POST[$name], function( $val ) {
                return '' !== $val;
            } );
    
            $has_value = ! empty( $vals );
        }
    
        if ( $tag->is_required() and ! $has_value ) {
            $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
        }
    
        return $result;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search