Stop WordPress automatically showing

tags

前端 未结 7 2321
猫巷女王i
猫巷女王i 2021-01-02 13:05

Wordpress automatically generate lot of unwanted

tags every where. Even the img tag also wrap by these

t

相关标签:
7条回答
  • 2021-01-02 13:27

    Wordpress Visual Editor itself create those tags for new lines.

    You can try to remove the filter wpautop from the_excerpt or the_content

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

    Sometimes it doesn't work (Didn't work for me in the past. Not with PHP).

    You can try with Javascript/jQuery, place this code after DOM loaded or before the closing </body> tag.

    jQuery('p:empty').remove();
    

    Will remove all empty <p></p> elements from all over the document.

    0 讨论(0)
  • 2021-01-02 13:32

    you can try this plugin

    http://wordpress.org/plugins/raw-html/
    

    or else user this in theme's functions.php file

    remove_filter( 'the_content', 'wpautop' );
    remove_filter( 'the_excerpt', 'wpautop' );
    
    0 讨论(0)
  • 2021-01-02 13:33

    You need to run the remove_filter function in the after_setup_theme hook. The reason been is that it might get overrun by later on by the content or excerpt. So you would use the function as follow

    function remove_the_wpautop_function() {
        remove_filter( 'the_content', 'wpautop' );
        remove_filter( 'the_excerpt', 'wpautop' );
    }
    
    add_action( 'after_setup_theme', 'remove_the_wpautop_function' );
    
    0 讨论(0)
  • 2021-01-02 13:33

    There are lots of plugin to disable autop in WordPress or you can use following filter to remove autop:

    remove_filter( 'the_content', 'wpautop' );
    

    However, if you need to style and format content from wp-editor, you will not be able to do so if you have removed autop.

    If you want to continue using autop functionality but want to remove empty p tag you can do it using jQuery.

    I am able to remove unwanted empty p tags using following codes:

    jQuery(document).ready(function(){
        $ = jQuery; 
        $('p').each(function() {
        var $this = $(this);
        if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
            $this.remove();
    }); 
    
    });
    
    0 讨论(0)
  • 2021-01-02 13:35
    $content = preg_replace('#^<\/p>|<p>$#', '', $content);
    echo do_shortcode($content);
    

    I tried it while using it in shortcode and it worked for me.

    0 讨论(0)
  • 2021-01-02 13:38

    I guess this will work fine-

    function my_format_TinyMCE( $in ) {
      $in['wpautop'] = false;
      return $in;
    }
    add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
    
    0 讨论(0)
提交回复
热议问题