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

Wordpress

Wordpress Problem Overview


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?

Wordpress Solutions


Solution 1 - Wordpress

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 - Wordpress

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 - Wordpress

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;

Solution 4 - Wordpress

Use this code to remove <p></p> before editor initialize.

function tinymce_remove_root_block_tag( $init ) {
    $init['forced_root_block'] = false; 
    return $init;
}
add_filter( 'tiny_mce_before_init', 'tinymce_remove_root_block_tag' );

Solution 5 - Wordpress

Removing the wpautop filter is not the most flexible solution. If you are looking for a page by page solution, or post by post, you can use this plugin:

https://wordpress.org/plugins/dont-muck-my-markup/

It will add a checkbox on every post/page for you to choose if the core-Wordpress behavior will be disabled for that particular page or not.

This is especially useful when you want to post some HTML code and the auto generated <br> and <p> mess up your carefully engineered design.

The plug-in also has a global option so you can have this behavior turned off by default on all pages/posts.

An alternative plug-in is https://wordpress.org/plugins/preserve-code-formatting/ however I only tried the one I described.

Solution 6 - Wordpress

As the accepted answer here didn't work for me in WP 4.8.1, I'm posting a workaround that worked for me. I just had to add a class to the p and WordPress won't remove it.

<p class="whatever">

Solution 7 - Wordpress

By default, WordPress adds paragraph <p></p> tags to category descriptions. Stop this by adding the following to your functions.php file

// Remove p tags from category description
remove_filter('term_description','wpautop');

Simple and easy (codeless).

Thank you

Solution 8 - Wordpress

use this $editor= wp_filter_nohtml_kses($_POST['editor1']);

Solution 9 - Wordpress

Sometimes removing WordPress's wpautop function in your theme's functions.php doesn't work (for some reasons).

So I have another solution how to stop removing <br> tags or double (<br><br>) line breaks.

  1. Make changes to your file /wp-content/themes/your_theme_name/functions.php

Add 2 lines to the top of your functions.

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

This will initially turn off wpautopop function.

  1. Make changes in file /wp-includes/formatting.php in function wpautop.

A) Change function wpautop( $pee, $br = true) to function wpautop( $pee, $br = false).

This will additionally turn off wpautopop function from all places.

B) Change $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee); to

    $pee1 = $pee;
    $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
 	$pee = $pee1;

This will prevent the system from removing double <br> tags. (I know the code is strange but simple //$pee doesn't help here because of ?> tag).

C) Change $pee = preg_replace("/\n\n+/", "\n\n", $pee); to //$pee = preg_replace("/\n\n+/", "\n\n", $pee);

This will prevent the system from removing multiple line breaks.

D) Change this:

    $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);

to that:

    //$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);

This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

E) Change this:

    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);

to that:

    //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);

Pretty the same: This will prevent the system from removing line breaks after the opening or before the closing block element tag like <div>, <article>, etc.

F) Change this:

    $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

to that:

    // $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);

This will prevent the system from removing <br> at the end of the block.

G) Change this:

    $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);

to that:

    //$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);

This will prevent the system from removing <br> after an opening or closing block tag.

Hope it will help! And read comments in this file – they will help you to understand what you need to turn on or turn off.

Solution 10 - Wordpress

in case you want to remove p tag in Wordpress post or textarea without a change in function, you can do a simple css-trick like : css :

.container >p {
display:none;
}

html:

<div class = 'container'>
<!the p tag u dont want here!>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNewUserView Question on Stackoverflow
Solution 1 - WordpressSantosh SView Answer on Stackoverflow
Solution 2 - WordpressDamien SellierView Answer on Stackoverflow
Solution 3 - WordpressGowriView Answer on Stackoverflow
Solution 4 - WordpressmyvahidView Answer on Stackoverflow
Solution 5 - Wordpressbg17awView Answer on Stackoverflow
Solution 6 - WordpressPablo SantamariaView Answer on Stackoverflow
Solution 7 - WordpressJodyshopView Answer on Stackoverflow
Solution 8 - WordpressAlpesh NavadiyaView Answer on Stackoverflow
Solution 9 - WordpressKir MazurView Answer on Stackoverflow
Solution 10 - Wordpressluan nguyenView Answer on Stackoverflow