【问题标题】:Format posted data in contact form 7 array在联系表格 7 数组中格式化发布的数据
【发布时间】:2022-01-01 10:28:13
【问题描述】:

我创建了一个名为[pureair] 的联系表格7 标记,其中显示了一个计算器。 用户可以添加房间和窗户来计算所需的值。

表单位于 Wordpress 插件样板部分内

<div id="depcore-pureair-caclulator" class='depcore-pureair-caclulator'>
    <section class="room" data-room-number='1'>
        <h3 class="room-title"><?= __('Room', 'depcore-pureair') ?></h3>
        <div class="room-fields">
            <p class="form-field"><label for="room-height-1"><?= __('Height', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-height-1]" id="room-height-1" min=1 step=1>&nbsp;cm</p>
            <p class="form-field"><label for="room-width-1"><?= __('Width', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-width-1]" id="room-width-1" min=1 step=1>&nbsp;cm</p>
            <p class="form-field"><label for="room-length-1"><?= __('Length', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-length-1]" id="room-length-1" min=1 step=1>&nbsp;cm</p>
            <p class="calculation-result"><label><?= __('Volume', 'depcore-pureair') ?></label> <span></span>m<sup>3</sup></p>
        </div>
        <div class="windows">
            <h3 class="window-title"><?= __('Glass', 'depcore-pureair') ?></h3>
            <div class="window" data-window-number="1">
                <p class="form-field"><label for="room-1-window-1-height"><?= __('Height', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-height]" id="room-1-window-1-height" min=1 step=1>&nbsp;cm</p>
                <p class="form-field"><label for="room-1-window-1-width"><?= __('Width', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-width]" id="room-1-window-1-width" min=1 step=1>&nbsp;cm</p>
                <p class="window-calculation-result"><label><?= __('Surface area', 'depcore-pureiar') ?></label><span></span>m<sup>2</sup></p>
                <div class="window-actions">
                    <div class="remove-window depcore-remove-button"><svg viewBox='0 0 30 29'>
                            <use xlink:href='#minus-icon'></use>
                        </svg><span><?= __('Remove window', 'depcore-pureair') ?></span></div>
                    <div class="add-window depcore-add-button"><svg viewBox='0 0 30 29'>
                            <use xlink:href='#plus-icon'></use>
                        </svg><span><?= __('Add window', 'depcore-pureair') ?></span></div>
                </div>
            </div>
        </div>
        <div class="depcore-pureair-warning">
            <p><?= __('This area is too small to effectively clear the room. Add a window', 'depcore-pureiar') ?></p>
        </div>
        <div class="room-actions">
            <div class="add-room depcore-add-button"><svg viewBox='0 0 28 29'>
                    <use xlink:href='#plus-icon'></use>
                </svg><span><?= __('Add room', 'depcore-pureair') ?></span></div>
            <div class="remove-room depcore-remove-button"><svg viewBox='0 0 28 29'>
                    <use xlink:href='#minus-icon'></use>
                </svg><span><?= __('Remove room', 'depcore-pureair') ?></span></div>
        </div>
    </section>
</div>

我正在使用过滤器$this-&gt;loader&gt;add_filter('wpcf7_special_mail_tags', $plugin_admin, 'calculator_wpcf7_pureair_mail_tag', 10, 3 ); 来显示电子邮件中的字段

public function calculator_wpcf7_pureair_mail_tag($output, $name, $html){
        $name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat

        $submission = WPCF7_Submission::get_instance();

        if (! $submission) {
            return $output;
        }

        if ('pureair' == $name) {
            return $submission->get_posted_data("pureair");
        }

        return $output;
    }

问题在于电子邮件中的值显示为逗号分隔的字符串(例如 270,200,300)。 我尝试使用 $this-&gt;loader&gt;add_filter('wpcf7_posted_data', $plugin_admin, 'calculator_wpcf7_posted_data'); 过滤器,但随后所有值都被删除了。

我想要实现的是遍历数组并在包含数据的电子邮件消息中创建格式化结果。例如

1号房间高:270cm,宽:200cm,长:400cm 视窗: 1:高:90cm,宽:110cm 费用:xxx 2号房间...

我已搜索但无法找到 ho 以获取过滤器内的数组值。

在深入研究了 Howard E 的建议后,我尝试如下使用 wpcf7_before_send_mail 来测试是否可以更改值

$submission = WPCF7_Submission::get_instance();

        if ($submission) {
            $posted_data = $submission->get_posted_data();
            foreach ($posted_data as $key => $value) {
                if($key == 'pureair') $posted_data['pureair'] = "<table><thead><tr><th>test</th></thead></table>";
                if($key == "your-name") $posted_data['your-name'] = "blabla@pl.pl";
            }

        }

但这不起作用。知道为什么吗?

【问题讨论】:

  • 我可能会使用wpcf7_before_send_mail 来自定义您的结果
  • 从来没有想过 - 我会试试这个。谢谢
  • 现在我正在尝试弄清楚如何调试数据
  • 那么现在,我的问题是您是否尝试更新已发送的邮件以包含您的阵列?您无法真正更改发布的值,但您可以更新已发送的邮件。你可以在这里看到一个例子stackoverflow.com/questions/70044554/…

标签: wordpress contact-form-7 wordpress-plugin-creation


【解决方案1】:

如果使用Smart Grid-layout 扩展名,可以通过一种方法来获得类似的结果。它允许您构建重复的选项卡式表单部分以及重复的字段表结构(请参阅此online tutorial,了解如何实现此目的)。

您的表单将有一个重复的房间选项卡部分,每个房间 1 个选项卡,字段高度/长度/宽度,以及每个选项卡中的表格结构用于窗口。该表格的每一行都有窗口高度/窗口宽度,允许用户向每个房间添加多个窗口。此外,每个选项卡还可以有一个只读的room-cost 字段。

然后,您的用户将能够添加多个房间标签,并在每个标签中添加房间的尺寸以及多行窗户。每次用户向表结构中添加新行时,都会在表上触发一个事件,允许您根据添加的房间/窗口在 room-cost 字段中显示计算值。

提交表单时,表格字段作为房间数组和窗口数组提交。您可以过滤这些字段在通知电子邮件中的显示方式(请参阅此tutorial)作为 HTML &lt;table/&gt;,这是在 HTML 邮件正文中显示表格结构的唯一方式,类似于这样

add_filter( 'cf7sg_mailtag_pureair', 'filter_cf7_mailtag_pureair', 10, 3);
function filter_cf7_mailtag_pureair($tag_replace, $submitted, $cf7_key){
  //$submitted an array of submitted fields
  if('my-pureair-form'==$cf7_key ){
    $tag_replace = '<table>'.PHP_EOL;
    foreach($submitted['room-height'] as $idx=>$height){ //tables_to_repair.
      $tag_replace .= '<tr><td>Room '.($idx+1).'</td>';
      $tag_replace .= '<td>height: '.$height.'cm, width: '.$submitted['room-width'][$idx].'cm, length: '.$submitted['room-length'][$idx].'cm </td>';
      foreach($submitted['window-height'][$idx] as $rdx=>$wheight){
        $tag_replace .= '<td>Windows: '.($rdx+1).': height: '.$wheight.'cm, width: '.$submitted['window-width'][$idx][$rdx].'cm </td>';
      }
      $tag_replace .= 'Cost: '.$submitted['room-cost'].'USD</tr>'.PHP_EOL;

    }
    $tag_replace .= '</table>'.PHP_EOL;
  }
  return $tag_replace;
}

【讨论】:

  • 当然,如果你能让它工作,请告诉我
猜你喜欢
  • 1970-01-01
  • 2011-10-29
  • 2021-08-19
  • 2018-07-18
  • 1970-01-01
  • 2014-10-08
  • 2018-04-16
  • 2020-06-07
  • 1970-01-01
相关资源
最近更新 更多