当类存在时,如何删除联系表单 7 中的跨度包装?

问题描述 投票:0回答:1

我正在尝试将

label
元素放入 'wpcf7-form-control-wrap'
span
,但我只希望在类 'floating' 添加到父 div 时完成此操作。

我正在使用以下解决方案:https://stackoverflow.com/a/72207202/16605585,但是使用此解决方案时,“wpcf7-form-control-wrap”

span
会受到每个字段的影响。

添加“浮动”类时,最终结果应该如下所示:

<div class="form-group floating">
  <span class="wpcf7-form-control-wrap" data-name="e-mailadres">
    <input size="40" class="wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email" id="e-mailadres" autocomplete="email" aria-required="true" aria-invalid="false" placeholder="E-mailadres" value="" type="email" name="e-mailadres">
    <label for="e-mailadres">E-mailadres</label>
  </span>
</div>

不添加“浮动”类的结果应该是:

<div class="form-group">
  <label for="e-mailadres">E-mailadres</label>
  <span class="wpcf7-form-control-wrap" data-name="e-mailadres">
    <input size="40" class="wpcf7-form-control wpcf7-email wpcf7-validates-as-required wpcf7-text wpcf7-validates-as-email" id="e-mailadres" autocomplete="email" aria-required="true" aria-invalid="false" placeholder="E-mailadres" value="" type="email" name="e-mailadres">
  </span>
</div>

有什么想法吗?

wordpress contact-form-7
1个回答
0
投票

深入挖掘后,我找到了解决方案:

add_filter('wpcf7_form_elements', function ($content) {
  $content = preg_replace('/<div class="(.*?\bfloating\b.*?)">\s+<span.*?>(.*?)<\/span>\s+<label.*?for="(.*?)">(.*?)<\/label>\s*<\/div>/s', '<div class="$1"><span class="wpcf7-form-control-wrap" data-name="$3">$2<label for="$3">$4</label></span></div>', $content);
  return $content;
});
© www.soinside.com 2019 - 2024. All rights reserved.