Contact Form 7 自动添加 p 标签

Contact Form 7 auto added p tags

我在 contact form 7 编辑器中有下一个代码

<div class="row">
    <div class="col-sm-8 col-sm-offset-2">
        <div class="row">
            <div class="col-sm-4">
                [text* name class:border-field placeholder "Name"]
            </div><!-- End of col -->
            <div class="col-sm-4">
                [email* email class:border-field placeholder "Email"]
            </div><!-- End of col -->
            <div class="col-sm-4">
                [text subject class:border-field placeholder "Subject"]
            </div><!-- End of col -->
        </div><!-- ENd of row -->
    </div><!-- End of col -->
</div><!-- ENd of row -->

<div class="row">
    <div class="col-sm-8 col-sm-offset-2">
        [textarea message class:border-field placeholder "Message"]
    </div>
</div><!-- End of row -->

<div class="row text-center">
    <div clas s="col-sm-12">    
        [submit class:btn class:btn-black-fill class:btn-small "Submit"]  
    </div><!-- End of col -->
</div><!-- End of row -->

问题是它几乎在每个元素之后添加了随机 p 标签,而且第一个文本字段由于某种原因比其他两个字段稍微高一点,而这两个字段应该都是内联的。而且我认为这不是 css 问题,因为之前我在平面 HTML 中编写了这个代码并且所有字段都是内联的所以我认为它必须与联系表 7 相关。

将此添加到您的 functions.php 文件中

function reformat_auto_p_tags($content) {
    $new_content = '';
    $pattern_full = '{(\[raw\].*?\[/raw\])}is';
    $pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
    $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($pieces as $piece) {
        if (preg_match($pattern_contents, $piece, $matches)) {
            $new_content .= $matches[1];
        } else {
            $new_content .= wptexturize(wpautop($piece));
        }
    }

    return $new_content;
}

remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');

add_filter('the_content', 'reformat_auto_p_tags', 99);
add_filter('widget_text', 'reformat_auto_p_tags', 99);

然后在您的 post 编辑器上用 raw 简码

包装您的联系表格 7 简码

例如

[raw][contact-form-7 id="1" title="Contact Us"][/raw]

根据 Contact Form 7 Docs,您可以通过在 wp-config.php 中放置以下常量来禁用插件的 "wpautop":

define( 'WPCF7_AUTOP', false );

如果编辑 wp-config.php 不是您的解决方案,这里有一个方便的过滤器。把它放在你的 functions.php:

// Remove <p> and <br/> from Contact Form 7
add_filter('wpcf7_autop_or_not', '__return_false');

关于这个我想说点什么,当我们想减少自动 P 标签形式时,我们应该使用下面的过滤器,只需在 function.php.

中编写 blow 代码
add_filter('wpcf7_autop_or_not', '__return_false'); 

我尝试了很多答案,但没有任何效果 所以...
我最终使用简单的 CSS 专门针对空 P 标签
像这样的形式:

.wpcf7-form p:empty { display: none; }

这对我有用,而且是一个简单的解决方案。

这也行。使用 WordPress 5.7、PHP 7.4、Contact Form 7 v5.4 进行测试。

<?php
add_filter('wpcf7_autop_or_not', false);

可能在某些情况下(旧版本的 WP,PHP?)需要使用 __return_false 效用函数。