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

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


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


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;

Tags:

Wordpress