Prevent TinyMCE wrapping text in p tags on WordPress

Sorry for my english, I’m not a native speaker.

I’ve been trying to solve this problem for hours with no luck so far.
The question looks like a duplicate of questions asked previously here on Stack Overflow, but none of the solutions found here worked for me.

Read More

TinyMCE wraps anything you put in there inside p tags. I need to change the forced_root_block setting of tinymce to prevent this but I have no idea on how to achieve that on WordPress.

I tried putting the code found here in my functions.php, but It did not work. Also, the problem doesn’t revolve around “wpautop”. It’s just a TinyMCE problem: it puts the p tags as a default behaviour because it needs a root block. In my case I don’t need a root block, because the editor’s content is just a plain text whose root block is in my template’s code. The added p tags simply break my layout, putting unnecessary html elements.

I tried using the TinyMCE advaced plugin, which has this option:

Stop removing the <p> and <br /> tags when saving and show them in the Text editor

but when this is not checked has the effect of “stripping” all the p tags, even the ones I intentionally use in the editor.

All I want to achieve is to prevent the editor from adding unwanted p tags and, at the same time, keep the p tags I intentionally use.

Is there a way to edit the forced_root_block setting of TinyMCE on WordPress?

Related posts

3 comments

  1. EDIT: After too many hours I was messing up between two different WordPress installations I was working on at the same time. The solution of the linked post worked for me, but I was checking the results on the other site. By putting this:

    function change_mce_options($init){
        $init["forced_root_block"] = false;
        $init["force_br_newlines"] = true;
        $init["force_p_newlines"] = false;
        $init["convert_newlines_to_brs"] = true;
        return $init;       
    }
    add_filter('tiny_mce_before_init','change_mce_options');
    

    in my functions.php, TinyMCE stopped putting anything inside p tags. The Stop removing the <p> and <br /> tags when saving and show them in the Text editor of TinyMCE Advaced needs to be checked.

  2. Add this filter to functions.php:

    function my_mce_options( $init ) {
        $init['wpautop'] = false;
        $init['indent'] = true;
        $init['tadv_noautop'] = true;
        return $init;       
    }
    add_filter( 'tiny_mce_before_init', 'my_mce_options' );
    

Comments are closed.