WordPress Version 3.3.1
qTranslate Version 2.5.28

qTranslate broke my Code Snippets when I was using the visual editor of WordPress. As soon as I’ve put a word inside <text>, the whole content between (and including) < > has been deleted. The content on the page however has been displayed correctly. It has “only” vanished from the editor. That’s not so useful of course, if you want to edit the page later on and you get only half of the contents…

First I thought, that I have to install a special code plugin such as WP-SynHighlight or SyntaxHighlighter Evolved to fix this issue. But with those Plugins the content has been removed too…

After doing some more research, I’ve noticed, that when qTranslate is disabled, everything works fine. So, I have checkt the sourcecode of qTranslate and found a solution 🙂 Just add the following Line and the problem is fixed 🙂

Solution 1 (obsolete)

Please do not use this solution anymore – because if you update your qtranslate plugin, the changes will get lost. Please use solution 2 below.

vi wp-content/plugins/qtranslate/qtranslate_wphacks.php 

        // remove wpautop
        if('html' != wp_default_editor()) {
                remove_filter('the_editor_content', 'wp_richedit_pre');
                // added by teslina. bugfix <>&
                add_filter('the_editor_content', 'wp_htmledit_pre');
        }

Solution 2 (current)

Here is the current solution. If you add this code to your functions.php, you don’t need to re-apply the changes if you update your qtranslate plugin. Thanks to Alok Jain for this tip  🙂

vi wp-content/themes/<themename>/functions.php 

add these lines of code:

        // added by teslina. bugfix <>&
// remove wpautop
        if('html' != wp_default_editor()) {
                remove_filter('the_editor_content', 'wp_richedit_pre');
                add_filter('the_editor_content', 'wp_htmledit_pre');
        }
// eof bugfix

And now… the ultimate TEST…

<b>My text is still here...</b>

🙂

To be sure, that the visual editor handles preformatted text correctly, I’ve modified my

wp-content/themes/THEMENAME/functions.php

and added the following code:

// Remove WordPress Auto P
remove_filter( 'the_content', 'wpautop' );

// function aus advanced tinymce plugin
if ( ! function_exists('tmce_replace') ) {
        function tmce_replace() {
                $tadv_options = get_option('tadv_options', array());
                $tadv_plugins = get_option('tadv_plugins', array());

?>
<script type="text/javascript">
if ( typeof(jQuery) != 'undefined' ) {
  jQuery('body').bind('afterPreWpautop', function(e, o){
    o.data = o.unfiltered
    .replace(/caption\]\ +?<\/object>/g, function(a) {
      return a.replace(/[\r\n]+/g, ' ');
    });
  }).bind('afterWpautop', function(e, o){
    o.data = o.unfiltered;
  });
}
</script>
<?php
        }
        add_action( 'after_wp_tiny_mce', 'tmce_replace' );
}
// eof advanced tinymce plugin

// http://tinymce.moxiecode.com/wiki.php/Configuration
function teslina_tinymce_config( $init ) {

// Change code cleanup/content filtering config

    // Don't remove line breaks
    $init['remove_linebreaks'] = false;
    // Convert newline characters to BR tags
    //$init['convert_newlines_to_brs'] = true;
    //$init['force_br_newlines '] = true;

    // With this option set to false, the line breaks are stripped from the HTML source.
    $init['apply_source_formatting'] = true;
    // Preserve tab/space whitespace
    $init['preformatted'] = true;
    // Do not remove redundant BR tags
    $init['remove_redundant_brs'] = false;

    return $init;
}
add_filter('tiny_mce_before_init', 'teslina_tinymce_config');

Flattr this!