Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

enter image description here

AFTER: enter image description here

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.

share|improve this question

3 Answers 3

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.

share|improve this answer
    
This will help with the iframes: public static function init_tinymce($init) { $init['extended_valid_elements'] .= ',pre[*],code[*],iframe[*]'; return $init; } add_filter('tiny_mce_before_init', 'init_tinymce'); –  Aram Kocharyan Mar 20 '12 at 23:07

Add to function.php

add_filter('tiny_mce_before_init', 'tiny_mce_before_init');

And function tiny_mce_before_init:

function tiny_mce_before_init($init) {
 $init['setup'] = "function(ed) {
     ed.onBeforeSetContent.add(function(ed, o) {
     if ( o.content.indexOf('<pre') != -1) {
     o.content = o.content.replace(/<pre[^>]*>[\\s\\S]+?<\\/pre>/g, function(a) {
     return a.replace(/(\\r\\n|\\n)/g, '<br />');
    });
   }
  });
 }";
 return $init;
}

http://core.trac.wordpress.org/ticket/19666 - it's known bug, there is workaround but not fix tabs only new line chars

share|improve this answer
    
Fixed in Wordpress 3.5+ –  OzzyCzech Feb 13 '13 at 10:44

This plugin seems to preserve white space between <pre> tags.

Preserve Code Formatting

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.