Removing

and
tags in WordPress posts

2019-01-06 10:01发布

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
9条回答
我想做一个坏孩纸
2楼-- · 2019-01-06 10:20

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.

查看更多
等我变得足够好
3楼-- · 2019-01-06 10:27

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' );
查看更多
该账号已被封号
4楼-- · 2019-01-06 10:28

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

查看更多
别忘想泡老子
5楼-- · 2019-01-06 10:30

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">
查看更多
We Are One
6楼-- · 2019-01-06 10:32

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

查看更多
beautiful°
7楼-- · 2019-01-06 10:37

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

查看更多
登录 后发表回答