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?
问题:
回答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
回答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
回答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;
回答4:
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.
回答5:
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' );
回答6:
use this $editor= wp_filter_nohtml_kses($_POST['editor1']);
回答7:
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">
回答8:
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.
- 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.
Make changes in file
/wp-includes/formatting.php
in functionwpautop
.A) Change
function wpautop( $pee, $br = true)
tofunction 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.
回答9:
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