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

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?


Solution 1:

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

Solution 2:

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

Solution 3:

try this

$my_postid = $post->ID;
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = strip_tags($content, '<p><br/>');
echo $content;