发送联系表格 7 中的所有下拉列表

时间:2021-06-14 10:34:33

标签: php wordpress contact-form-7

我需要将完整的自定义字段数组发送到带有联系表格 7 的邮件(动态填充),以便在发送之前在此处工作:

// 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);

因为是在发送邮件之前,我无法在发送之前调用任何东西进行比较。

所以我想发送隐藏输入中的所有数据以进行比较。

这是联系表格7中的两个输入:

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

我这里的目标是发送隐藏选择的所有数据。

我没有找到通过邮件发送所有输入的方法。

有可能吗?有更好的方法吗?

谢谢

编辑:

我的问号2:

目标是发送一封带有会话日期和 ID 的邮件。

我使用 ACF,我有:

enter image description here

在动态下拉之后,对于用户来说是这样的:

enter image description here

问题是我没有会话的 ID,只有日期。

要知道我需要与所有自定义字段的数组进行比较的 id,我无法在 wpcf7_posted_data 期间导入它。

我想如果我在隐藏字段中发送数组的所有数据,我可以重新制作数组并找到我的用户选择的会话的 ID。

我希望我说得更清楚。

(我无法在 wpcf7_posted_data 期间在 php 中发出请求。我可以发出 ajax 请求吗?)

编辑2: 这是我的带有会话和文本的隐藏选择 enter image description here

这是联系表单 7 的 html,其余是 CSS 的 div [select upcoming-date data:date id:date] [hidden select upcoming-date2 data:date2]

编辑3: 好的,明白了。

我用来制作下拉菜单的自定义字段分为 id 和 text 两部分。我有需要 ID 的文本部分。

如果我发送邮件中的每个文本和 ID,我可以与用户的回答进行比较,然后将正确的 ID 添加到邮件中。

这里生成的html:http://www.sharemycode.fr/5ax

编辑 4: 我写下拉列表的 id 和文本的地方:

enter image description here

我创建选择的地方:

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 个答案:

答案 0 :(得分:1)

看起来 Contact Form 7 本身就支持这一点,只是关于如何实现它并不是很明显。

以下是解释该功能的文档页面:http://contactform7.com/selectable-recipient-with-pipes/

基本上你所要做的就是像这样输入值:

可见值|actual-form-value

管道“|”之前是什么字符将显示在表单中,后面的将是为表单填写的实际值。

编辑卡纳普: 我在这里添加我的代码来分隔 HowardE 的答案。

这就是我动态创建选择的方式:

   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;
    }

它不起作用,我使用 Smart Grid-Layout Design for Contact Form 7 来动态创建我的选择

答案 1 :(得分:1)

我会为选择创建一个自定义表单标签。以下代码将创建一个名为 [gigs] 的自定义表单标签,其用法如下:

[gigs upcoming-gigs]

我还添加了添加 * 并使其成为必需的功能。

我的假设是您实际上是如何获取 ACF 字段的,而我实际上无法做到,因为我没有它们,而且您还没有完全分享它的存储方式。您可以将其添加到您的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;
}