简体   繁体   中英

Stop WordPress automatically showing <p></p> tags

Wordpress automatically generate lot of unwanted <p></p> tags every where. Even the img tag also wrap by these <p> tags. So It's create unwanted white spaces in the site. I tried lot of ways to remove those tags:-

preg_replace(array('<p>','</p>'),array('',''),the_content(),1);

remove_filter( 'the_content', 'wpautop' );

preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content)

nothing works for me. First I want to know why those tags automatically generating? And How can I fix that?

Wordpress Visual Editor itself create those tags for new lines.

You can try to remove the filter wpautop from the_excerpt or the_content

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

Sometimes it doesn't work (Didn't work for me in the past. Not with PHP).

You can try with Javascript/jQuery, place this code after DOM loaded or before the closing </body> tag.

jQuery('p:empty').remove();

Will remove all empty <p></p> elements from all over the document.

you can try this plugin

http://wordpress.org/plugins/raw-html/

or else user this in theme's functions.php file

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

You need to run the remove_filter function in the after_setup_theme hook. The reason been is that it might get overrun by later on by the content or excerpt. So you would use the function as follow

function remove_the_wpautop_function() {
    remove_filter( 'the_content', 'wpautop' );
    remove_filter( 'the_excerpt', 'wpautop' );
}

add_action( 'after_setup_theme', 'remove_the_wpautop_function' );

There are lots of plugin to disable autop in WordPress or you can use following filter to remove autop:

remove_filter( 'the_content', 'wpautop' );

However, if you need to style and format content from wp-editor, you will not be able to do so if you have removed autop.

If you want to continue using autop functionality but want to remove empty p tag you can do it using jQuery.

I am able to remove unwanted empty p tags using following codes:

jQuery(document).ready(function(){
    $ = jQuery; 
    $('p').each(function() {
    var $this = $(this);
    if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
        $this.remove();
}); 

});

Go into the theme editing area of WP admin.

Add this code to your functions.php file, and save it.

<?php
function my_formatter($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', 'my_formatter', 99);
?>

Now, when writing a post/page, use [raw][/raw] tags to surround that parts of the post/page that you do not want formatted.

OR

 <?php the_content(); ?>

and place this directly above:

<?php remove_filter ('the_content', 'wpautop'); ?>

it should look like this:

<?php remove_filter ('the_content', 'wpautop'); ?>

<?php the_content(); ?>
$content = preg_replace('#^<\/p>|<p>$#', '', $content);
echo do_shortcode($content);

I tried it while using it in shortcode and it worked for me.

I guess this will work fine-

function my_format_TinyMCE( $in ) {
  $in['wpautop'] = false;
  return $in;
}
add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM