tinyMCE duplicates previous block element when pressing return (visual editor)

Going crazy with this. Can’t figure out if this is normal behavior.

I have added a filter to rewrite image inserts to use <figure> and <figcaption>. Works well.

However, when I insert an image in the Visual view and press return, I want it to start a new paragraph.

Instead it creates another <figure> element and puts my next line of text in that.

Example of the code produced if switch to Text view:

<figure class="alignnone">

<a href="https://wordpress.stackexchange.com/questions/83997/path/to/image.jpg"><img class="alignnone size- wp-image-209" src="https://wordpress.stackexchange.com/questions/83997/path/to/image.jpg" width="199" height="43" /></a>

<figcaption>Image Caption</figcaption>

</figure>

<figure class="alignnone"`>This should be a new paragraph but is inside a figure tag.</figure>

Is there any way I can stop tinyMCE from replicating the previous block element? I know if you set some text as, say, an <h2> and press return the next line defaults back to <p>. That’s the behavior I want.

4 Answers
4

I’m currently having the same problem. Here’s my work-around.

add_filter( 'tiny_mce_before_init', 'workaround' );
public function workaround( $in ) {
    $in['force_br_newlines'] = true;
    $in['force_p_newlines'] = false;
    $in['forced_root_block'] = '';
    return $in;
}

tiny_mce_before_init gives you access to the TinyMCE settings that WordPress’ editor uses. See also: TinyMCEConfiguration

The downside to this is instead of “return” resulting in a p it instead gives you a br. I’ve tried reversing the force_br_newlines and force_p_newlines but it results in the original problem. Hope this is of some help.

Leave a Comment