Remove automatic p tag in WordPress

2019-06-07 04:39发布

Before all, I've searched a way to done this. But it still occure.

So I use the theme Creative, I've got a contact form, done with Contact Form 7. And to display it in the right place, it need to be placed in a widget aera.

I've inserted the shortcode of the form in a text widget, in a widget for shortcode, and also in a widget for Contact Form 7.

For every case, the inputs and label are wrapped into p tag, and it brake the submit function, I guess.

So I've tried a fiew things to disable this function.

In my function.php, at the end :

//* Disable automatic p tag insertion
remove_filter( 'the_content', 'wpautop' );
remove_filter('the_excerpt', 'wpautop');
add_filter( 'the_content', 'disable_wpautop_cpt', 0 );
function disable_wpautop_cpt( $content ) {
  'your_cpt_slug' === get_post_type() && remove_filter( 'the_content', 'wpautop' );
return $content;
}

In my wp-config.php, at the end too :

/** Disable automatic p tag insertion */
define( 'WPCF7_AUTOP', false );

And more, I added a plugin to disable this function of WP on the page where the form is.

But the tags are still there. Help

EDIT : Added screen.

Form code

HTML in inspector

标签: wordpress
2条回答
smile是对你的礼貌
2楼-- · 2019-06-07 05:05

Use wpautop filter for remove auto p tags in content or the_excerpt

<?php wpautop( $foo, $br ); ?> 

Example:

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

More Ref : https://codex.wordpress.org/Function_Reference/wpautop

查看更多
成全新的幸福
3楼-- · 2019-06-07 05:14

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]
查看更多
登录 后发表回答