tinymce adding p tags automatically?
Asked Answered
V

6

50

Im using tinymce and saving it to a database.

When I edit the saved content using tinymce and save again, it inserts a p tag at the beginning.

Editing the content over and over again leaves a large gap at the beginning of the content.

DOes anyone know a fix?

Vouge answered 12/12, 2012 at 14:35 Comment(0)
G
106

TinyMce automatically add "<p>" in new lines. You can set this option in tinyMce initialization like this:

  tinyMCE.init({
      mode : "textareas",
      theme : "advanced",
      force_br_newlines : false,
      force_p_newlines : false,
      forced_root_block : '',
  });

Hope it will help

Fonski

Galimatias answered 12/12, 2012 at 14:42 Comment(4)
forced_root_block: '' stops tinymce for constantly wrapping my <span> tags with <p>. Thank you!Calomel
Now it adds DIV tags instead :(Castile
slott, I think this is a recent update in v.4 Try also adding invalid_elements:"div" to wipe out the extra divs.Ruhnke
Have to make forced_root_block: '' in tinymce.min.js also. This only makes stop adding <p> tag from the textarea in TinyMce editorOtes
E
6

For me it worked by making "force_br_newlines : true" instead of false.

 tinyMCE.init({
      mode : "textareas",
      theme : "advanced",
      force_br_newlines : true,
      force_p_newlines : false,
      forced_root_block : ''
  });

I hope it helps

Expatriate answered 28/10, 2016 at 11:32 Comment(1)
thanks. I wondered why the selected answer's not working for meAuthorized
N
4

I am sure that @Fonski answer is correct but thought I would update this for anyone else that did was confused as to where to put the code. I placed the following in my _config.php file to get it to work:

$defaultEditorConfig = HtmlEditorConfig::get('cms');
$defaultEditorConfig->setOptions(
    array(
        'mode'              => 'textareas',
        'theme'             => 'advanced',
        'force_br_newlines' => false,
        'force_p_newlines'  => false,
        'forced_root_block' => ''
    )
);

Note: If you just want to remove the p tag that automatically wraps image tags (etc) all you need to set is the 'forced_root_block' => '' option.

EDIT: This advice is for those using SilverStripe, I posted this thinking the questions was SilverStripe specific.

Nahshu answered 23/2, 2015 at 0:45 Comment(0)
K
2

I have been working with TinyMCE v7 recently and encountered this issue. However the force_*_new_lines does no longer work. After a bit of reading through the docs I found the solution here. Although the issue is already solved, I figured adding this for anyone else searching.

TinyMCE v6.1 and up use the following to for shift_enter behavior on every enter

newline_behavior: 'linebreak'
Kraul answered 27/3, 2023 at 12:33 Comment(0)
F
0

From tinymce.js v4.1.10 code:

newBlockName = (settings.force_p_newlines ? 'p' : '') || settings.forced_root_block;

So the key to avoid <p> seems to be as stated before

settings.force_p_newlines = false

and

settings.forced_root_block = ''
Forensics answered 18/8, 2017 at 19:14 Comment(0)
T
0

using @tinymce/tinymce-react with ^4.3.2 version use any string in forced_root_block, but not empty string worked for me

init:{{ height: 600, forced_root_block: "anything", }}

Tyrothricin answered 19/2 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.