Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 308
batMask Avatar asked Mar 28 '14 10:03

batMask


3 Answers

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' );
like image 108
Pieter Goosen Avatar answered Sep 28 '22 08:09

Pieter Goosen


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.

like image 37
Rahil Wazir Avatar answered Sep 28 '22 08:09

Rahil Wazir


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' );
like image 38
Ananth Avatar answered Sep 28 '22 07:09

Ananth