How can I remove p tags that are auto added within tinymce

前端 未结 11 1879
挽巷
挽巷 2020-12-06 04:46

I am using tinymce 4.0.1 and it automatically adds p tags when you either start typing or hit enter. How can I dynamically remove these p tags and then reinsert the content

相关标签:
11条回答
  • 2020-12-06 04:50

    Add this to your functions.php file and the standard

    tags will be removed by adding some parameters to the tiny_mce_before_init hook. If you want to see how it works, you can read further on this page: https://codex.wordpress.org/TinyMCE

    ////////////////////////////////////////////////////////////////////////
    //////////REMOVE STANDARD <P> FROM TINYMCE EDITOR/////////////////////////
    ///////////////////////////////////////////////////////////////////////
    function my_format_TinyMCE( $in ) {
    $in['forced_root_block'] = "";
    $in['force_br_newlines'] = TRUE;
    $in['force_p_newlines'] = FALSE;
    return $in;
    }
    add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
    
    0 讨论(0)
  • 2020-12-06 04:52

    Insert in theme functions.php the following code:

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

    If you want replace root "p" tag with some else, replace false with "div" (for example).

    0 讨论(0)
  • 2020-12-06 04:58

    Tinymce needs a root block element, which is a paragraph by default, in order to be able to style content. So removing those paragraphs will only result in all content being wrapped into one single paragraph because tinymce is forced to use this as root block element.

    0 讨论(0)
  • 2020-12-06 05:00

    If you use jQuery could do:

    $("p").remove();
    
    0 讨论(0)
  • 2020-12-06 05:02

    you need to add the following line to your init statement

    forced_root_block : ""
    

    So, your complete code will be like this:

        <script>tinymce.init({forced_root_block : "",selector:'textarea'});</script>
    
    0 讨论(0)
  • 2020-12-06 05:02

    You need to add the following line to your init statement.

    forced_root_block : ""
    
    0 讨论(0)
提交回复
热议问题