Skip to main content
added 289 characters in body
Source Link
bueltge
  • 17.1k
  • 7
  • 61
  • 97

At first, I think this problem was solved since WP version 3.5; see ticket 19666 in trac. But the tinyMCE have a hook there give us the chance to change the content inside the editor and you must not parse on output on frontend.

A small source script. I have no test this with a current WP version, was a older solution for a customer.

Add this source via plugin and enhance the markup. The function check for the html-tag <pre and if exists, then will be replace with markup.

add_filter( 'tiny_mce_before_init', 'fb_correction_content_tiny_mce' );

function fb_correction_content_tiny_mce( $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;
}

A small source script. I have no test this with a current WP version, was a older solution for a customer.

Add this source via plugin and enhance the markup. The function check for the html-tag <pre and if exists, then will be replace with markup.

add_filter( 'tiny_mce_before_init', 'fb_correction_content_tiny_mce' );

function fb_correction_content_tiny_mce( $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;
}

At first, I think this problem was solved since WP version 3.5; see ticket 19666 in trac. But the tinyMCE have a hook there give us the chance to change the content inside the editor and you must not parse on output on frontend.

A small source script. I have no test this with a current WP version, was a older solution for a customer.

Add this source via plugin and enhance the markup. The function check for the html-tag <pre and if exists, then will be replace with markup.

add_filter( 'tiny_mce_before_init', 'fb_correction_content_tiny_mce' );

function fb_correction_content_tiny_mce( $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;
}
Source Link
bueltge
  • 17.1k
  • 7
  • 61
  • 97

A small source script. I have no test this with a current WP version, was a older solution for a customer.

Add this source via plugin and enhance the markup. The function check for the html-tag <pre and if exists, then will be replace with markup.

add_filter( 'tiny_mce_before_init', 'fb_correction_content_tiny_mce' );

function fb_correction_content_tiny_mce( $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;
}