Contact Form 7 auto added p tags

╄→гoц情女王★ 提交于 2019-12-03 04:31:55

According to the Contact Form 7 Docs, you can disable "wpautop" for the plugin by placing the following constant in wp-config.php:

define( 'WPCF7_AUTOP', false );

If editing wp-config.php is not the solution for you, there's a handy filter. Put it in your functions.php.

add_filter('wpcf7_autop_or_not', '__return_false');

Hitesh Prajapati

I would like to say something about this, when we want reduce auto P tag form the we should go with below filter and just write blow code in function.php.

add_filter('wpcf7_autop_or_not', '__return_false'); 

Add this in your functions.php file

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

Then on your post editor wrap your contact form 7 shortcode with raw shortcode

e.g.

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

I tried many answers but nothing worked so...
I ended up using simple CSS to specifically target empty P tags
in the form itself like this:

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

This worked for me and, its a simple solution.

Follow-up to rnevius answer, put this in /wp-content/plugins/contact-form-7/wp-contact-form-7.php

if ( ! defined( 'WPCF7_AUTOP' ) ) {
    define( 'WPCF7_AUTOP', false );
}

Normally, it's already there with a true value, in that case, just replace false by true and you're good to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!