Tabs placed in a <pre><code></code></pre>
block are stripped and line breaks are removed leaving a single continuous line of text. This occurs when switching from html to visual editors. Is there a setting in TinyMCE to avoid this?
BEFORE:
AFTER:
Here it even garbles the code and spits parts of it out the <pre>
…
EDIT:
I seems the best option is to remove the <code>
tag entirely and leave the pre. This still has the issue of removing duplicate blank lines, but it keeps the pre untouched even when switching between editors.
Add to function.php
And function tiny_mce_before_init:
http://core.trac.wordpress.org/ticket/19666 – it’s known bug, there is workaround but not fix tabs only new line chars
I have also added custom shortcodes to functions.php to avoid things breaking when switching from html to visual editors. Mainly I’ve used this for iFrame’s, but maybe it would be helpful here too.
This plugin seems to preserve white space between
<pre>
tags.Preserve Code Formatting
For anyone who struggles with pasting or working with formatted
<pre>
code in Visual editor. This solution has been working for me many years, and I love it. This approach uses a “middle-hand” to serve correct pre-content and characters between the editors andwpautop()
frontend.Just paste into functions.php
It seems heavy, but thats why jQuery must be in global scope as it talks with tinMCE API. The dollarsign
$
can NOT be in use and thejQuery
text clutter the code down. Its mostly the creation of HTML elements that takes space.How it works: Pretty simple, Without “touching” it -TinyMCE dont mess with it. Its worth trying out!
This approach is not a hack or a corny preg match solution. It produces the same converastions as default API.
The PUSH cleanup works if you paste a code from a PHP class or whatever and the chunk or cutted out function you wanna present, has a lot of space or tabs “before”. It calculates the space that occurs on the first existing line.
Makes this:
Into This:
B.T.W, it comes with a handy focus button, appears at bottom right of editor and is useful when working with large amount of
<pre>
code chunks. Turn off screen settings “Enable full-height editor and distraction-free functionality.” for better UI.