Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing <p> and <br/> tags in WordPress posts

Tags:

wordpress

Whenever I am posting some content in WordPress post page it is showing some paragraph tags like <p> and <br/>. Which is showing some extra space in output. So is there any solution for it? How to remove all the tags?

like image 238
NewUser Avatar asked Jul 08 '11 13:07

NewUser


People also ask

How do I get rid of unwanted P tags on WordPress?

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.

How do I stop WordPress from removing line breaks?

Create a WordPress Line Break (br) Shortcode You can add a shortcode to fix the line breaks not working issue in WordPress. Insert the following shortcode code into the functions. php file: function add_linebreak_shortcode() { return '<br />'; } add_shortcode('br', 'add_linebreak_shortcode' );


2 Answers

This happens because of WordPress's wpautop. Simply add below line of code in your theme's functions.php file

remove_filter( 'the_content', 'wpautop' );

remove_filter( 'the_excerpt', 'wpautop' );

For more information: http://codex.wordpress.org/Function_Reference/wpautop

like image 140
Santosh S Avatar answered Sep 24 '22 23:09

Santosh S


I would not recommend using the remove_filter option since it will remove tags and markups everywhere you call them in your template.

Best way is to sanitize your string through PHP

you can use php function strip_tags to remove useless markups: echo strip_tags(get_the_excerpt()); // Get the raw text excerpt from the current post

or

echo strip_tags(category_description()); // Get the raw text cat desc from the current cat page

this will remove all HTML tags from the current wordpress post when calling it.

you can also add the trim function just in case of: echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post

or

echo trim(strip_tags(category_description())); // Get the raw text cat desc from the current cat page

this is perfect to get raw text for meta="description" and title where HTML markups are not recommanded.

Hope it helps

like image 23
Damien Sellier Dubois Avatar answered Sep 25 '22 23:09

Damien Sellier Dubois