首页 文章

联系表格7收件人的动态数量

提问于
浏览
7

我在一个表格上工作,其核心是两个字段集:主要和“其他收件人”;在“其他收件人”字段集的末尾,我有一个“添加另一个收件人”链接 .

这是谁需要什么:

Main recipient: 一切

Other recipient: "other recipient" fieldset;

Sub-sequent recipients: 各个字段集

到目前为止,我一直在看Documentation,但那里没有多少运气,也不是我所期待的 .


编辑

我认为这还不清楚,所以我会更明确地说明背景是什么 . 我的 form 是一个注册,我们可以注册多个人;其中一个字段标记为"Your email" . 由于我们可以同时注册多个人,因此我需要复制包含"Your email"的 fieldset .


编辑2

为了帮助澄清,想象一下,我们正在为夏令营注册孩子 . 第一个 fieldset 是通用的,比如父母的账单信息,第二个 fieldset 是孩子的信息 . 父母需要能够填写单个 form 并动态添加父母所需的子项 .

在每个孩子的 fieldset 中,他们的电子邮件是必需的,他们会收到与这个孩子相关的信息,其中的电子邮件类似于:

Hello {children's name},

You've been registered to StackOverflow Summer Camp. Here's the information you need to know:

[...]

Thanks for being a good sport!

希望这可以帮助 .

3 回答

  • 3

    当你有这样一个特定的用例时,将功能强加到外围相关的插件中往往会导致挫败感 . 话虽这么说 - 有时你会嫁给一个特定的插件或方法,只需要 Build 在它之上 .

    有了这个警告,我认为你应该从联系表格7的 creating a new fieldtype 角度处理这个问题 . 这样你就可以控制在另一个答案的评论中提到的字段's HTML, the data validation, among other things. It might also provide an launch point for DB storage and sending reminders, later, as you' .

    以下是这种方法:

    Testing CF7 Duplication

    新的fieldtype称为 emailplus ,并将其包含在如下形式中:

    <div class="cf7-duplicable-recipients">
        <label>Main Recipient (required)</label>
        [emailplus emails]
        [submit "Sign Up"]
    </div>
    

    另外,我've set the recipient under the 1707025 panel in the form'的设置为 [emails] .

    如果将 emailsplus 字段设置为收件人,则该类将覆盖默认的wpcf7行为,并将邮件发送到 email 数组中的每个值,并在每封电子邮件的基础上替换邮件正文中的任何[电子邮件]占位符 .

    emailplus 字段类型在这里封装在一个类中,并且自由地评论:

    <?php
    
    class WPCF7_Duplicable_Email
    {
        /**
         * The emails this form's field is addressed to
         *
         * @var array
         */
        public $emails = array();
    
        /**
         * The name of the tag bearing the emailplus type
         *
         * @var string
         */
        public $tag_name;
    
        /**
         * Instantiate the class & glom onto the wpcf7 hooks
         *
         * @return void
         */
        public function __construct()
        {
            add_action('wpcf7_init', array($this, 'add_emailplus_tag'));
            add_action('wpcf7_form_tag', array($this, 'assign_values_to_field'));
            add_filter('wpcf7_validate_emailplus', array($this, 'validate'), 2);
            add_action('wpcf7_before_send_mail', array($this, 'send_email'));
            add_action('wp_enqueue_scripts', array($this, 'emailplus_scripts'));
        }
    
        /**
         * Add our the [emailplus __YOUR_FIELD_NAME__] shortcode for use in wpcf7 forms.
         *
         * @uses wpcf7_add_shortcode
         *
         * @return void
         */
        public function add_emailplus_tag()
        {
            wpcf7_add_shortcode(
                array('emailplus'),
                array($this, 'shortcode_handler'),
                true
            );
        }
    
        /**
         * Renders the HTML for the [emailplus] shortcode
         *
         * Referenced from wpcf7_text_shortcode_handler in wp-content/plugins/contact-form-7/modules/text.php
         *
         * @param array $tag The data relating to the emailplus form field.
         *
         * @uses wpcf7_get_validation_error
         * @uses wpcf7_form_controls_class
         *
         * @return string
         */
        public function shortcode_handler(array $tag) {
            $tag = new WPCF7_Shortcode($tag);
    
            if (true === 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(
                'class'        => $tag->get_class_option($class),
                'id'           => $tag->get_id_option(),
                'tabindex'     => $tag->get_option('tabindex', 'int', true),
                'aria-invalid' => $validation_error ? 'true' : 'false',
                'type'         => 'email',
                'name'         => $tag->name.'[]', // <-- Important! the trailing [] Tells PHP this is an array of values
                'value'        => $tag->get_default_option()
            );
    
            $atts = wpcf7_format_atts($atts);
    
            $html = sprintf(
                '<div class="emailplus-wrapper %1$s"><input %2$s />%3$s</div>',
                sanitize_html_class($tag->name),
                $atts,
                $validation_error
            );
    
            // We identify the container that will hold cloned fields with the [data-wpcf7-duplicable-email] attr
            return '<div class="wpcf7-form-control-wrap %1$s" data-wpcf7-duplicable-email>'.$html.'</div>';
        }
    
        /**
         * Validates the value of the emailplus tag.
         *
         * Must be handled separately from other text-based form inputs,
         * since the submitted POST value is an array.
         *
         * We can safely assume emailplus while creating the WPCF7_Shortcode,
         * because we know we hooked this function onto 'wpcf7_validate_emailplus'
         *
         * @uses wpcf7_is_email
         * @uses WPCF7_Validation::invalidate
         *
         * @param WPCF7_Validation $result The validation helper class from wpcf7.
         * @param array            $tag    The array of values making up our emailplus tag
         *
         */
        public function validate(WPCF7_Validation $result, $tag)
        {
            $tag = new WPCF7_Shortcode(
                array(
                    'basename' => 'emailplus',
                    'name' => $this->tag_name,
                    'raw_values' => $this->emails
                )
            );
    
            // Check each value of the form field.
            // Emails must be validated individually.
            foreach($tag->raw_values as $email) {
                if (false === wpcf7_is_email($email)) {
                    $result->invalidate($tag, wpcf7_get_message('invalid_email'));
                }
            }
    
            return $result;
        }
    
        /**
         * For completeness' sake, manually assign the value to the emailplus fieldtype.
         *
         * Wpcf7 doesn't know how to treat our fieldtype's value by default.
         *
         * As a side effect, this sets the email addresses that are up for delivery.
         *
         * @param array $scanned_tag The tag that wpcf7 is scanning through, and processing.
         *
         * @return $array;
         */
        public function assign_values_to_field($scanned_tag)
        {
            if ($scanned_tag['type'] !== 'emailplus') {
                return $scanned_tag;
            }
    
            $this->tag_name = $scanned_tag['name'];
    
            if (key_exists($scanned_tag['name'], $_POST)) {
                // Stores the emails on a class property for use later.
                $this->emails = $_POST[$scanned_tag['name']];
                $scanned_tag['raw_values'] = $this->emails;
                $scanned_tag['values']     = $this->emails;
            }
    
            return $scanned_tag;
        }
    
        /**
         * Performs a substitution on the emailplus field's fieldname, on a per-value basis.
         *
         * Ex. in two parts
         *  1 - The shortcode [emailsplus emails] renders into <input type="email" name="emails[]" value="" >
         *      Which the user clones and submits, processing into something like
         *      ['test1@gmail.com', 'test2@gmail.com'].
         *  2 - The user has set [emails] as the recipient in the "mail" panel for the form.
         *
         * Because wpcf7 isn't aware of how to process a field w/ multiple values when emailing,
         * we loop over the values of [emails], replace the tag, and invoke WPCF7_Mail::send()
         * for each value.
         *
         * @param WPCF7_ContactForm $form The contact form object.
         *
         * @uses WPCF7_Mail::send
         *
         * @return void
         */
        public function send_email(WPCF7_ContactForm $form)
        {
            $placeholder = '['.$this->tag_name.']';
    
            if (false === strpos($form->prop('mail'), $placeholder)) {
                return;
            }
    
            foreach ($this->emails as $email) {
                $template = $form->prop('mail');
                $template['recipient'] = str_replace($placeholder, $email, $template['recipient']);
                $template['body']      = str_replace($placeholder, $email, $template['body']);
                WPCF7_Mail::send($template);
            }
    
            // Tell cf7 to skip over the default sending behaviour in WPCF7_Submission->mail()
            $form->skip_mail = true;
        }
    
        /**
         * Adds our js that will clone the emailplus field.
         *
         * Could be optimized with a conditional that checks if there is a form with the [emailplus]
         * fieldtype somewhere on the page
         *
         * @return void
         */
        public function emailplus_scripts()
        {
            wp_enqueue_script(
                'cf7-duplication',
                get_template_directory_uri() . '/js/cf7-duplication.js',
                array('jquery'),
                '20161006',
                true
            );
        }
    }
    
    $wpcf7DuplicableEmail = new WPCF7_Duplicable_Email();
    

    并且,处理克隆的.js文件 . 它应该住在 /path/to/your/theme/js/cf7-duplication.js' .

    (function($) {
      $(document).ready(function() {
        var addEmailField = function(inputRow, container) {
          inputRow.find('input[type=email]').val('');
    
          var removeButton = $('<a href="#">&times;</a>')
            .click(function(e) {
              e.preventDefault();
              inputRow.remove();
            });
    
          inputRow.append(removeButton);
          inputRow.insertAfter(container.find('.emailplus-wrapper').last());
        }
    
        $.each($('[data-wpcf7-duplicable-email]'), function(i, el) {
          var container = $(el);
          var inputRow  = container.find('.emailplus-wrapper');
          var addButton = $('<a href="#">Add another email</a>');
    
          addButton.click(function(e) {
            e.preventDefault();
            var newEmailField = inputRow.clone();
            addEmailField(newEmailField, container);
          });
    
          container.append(addButton);
        });
      });
    })(jQuery);
    

    最后,但并非最不重要的是,如果您希望表单在淡出时有效并且电子邮件已经消失,请将其添加到“其他设置”面板中 .

    on_sent_ok: "jQuery('.cf7-duplicable-recipients').fadeOut();"
    

    这种方法最适合CF7的AJAX提交 . 如果要扩展它以处理vanilla POST请求,可以更新短代码处理程序以呈现多个 <input> 字段,您需要在无效提交时保留 value attr .

  • 0

    选项1)在设置菜单中的邮件点按下,单击邮件(2)后,在TO:字段中添加此行 . 如果父母的孩子少于所述的孩子数量,额外的电子邮件地址应该什么都不做 . [email],[email]是基本格式 .

    [parents-email], [kid-email1], [kid-email2], [kid-email3], [kid-email4], [kid-email5], [kid-email6], [kid-email7]
    

    选项2)在TO:字段中,只需放置1个电子邮件地址,例如父母 . 并在附加 Headers 中:将代码放在下面

    CC: [kid-email1], [kid-email2], [kid-email3], [kid-email4], [kid-email5], [kid-email6], [kid-email7]
    

    要么

    BCC: [kid-email1], [kid-email2], [kid-email3], [kid-email4], [kid-email5], [kid-email6], [kid-email7]
    

    可能出现的问题:许多主机阻止这样的事情以防止垃圾邮件 . 如果这些不起作用,那么可能就是这种情况 . 您需要联系您的托管服务提供商以了解删除该块 .

  • -3

    联系表单7只有一个收件人字段,但您可以在该字段中输入多个以逗号分隔的电子邮件地址,例如“email1 @ domain.com,email2 @ domain.com,email3 @ domain.com” .

    因此,对于您的情况,使用JavaScript动态添加多个重复的收件人字段,最后在表单上提交编写JavaScript函数以连接所有收件人电子邮件地址,然后将其保留在主收件人字段中并提交表单 . 希望你明白我的意思 .

相关问题