Skip to content

Instantly share code, notes, and snippets.

@bueltge
Created January 23, 2012 15:03
Show Gist options
  • Save bueltge/1663554 to your computer and use it in GitHub Desktop.
Save bueltge/1663554 to your computer and use it in GitHub Desktop.
Code to Question on WPSE questions 39843
<?php
/**
* Plugin Name: Remove br in pre
* Plugin URI: http://wordpress.stackexchange.com/questions/39843/no-filter-of-code-on-switch-from-html-to-visual-editor-how
* Text Domain:
* Domain Path: /languages
* Description:
* Version: beta
* Author: Frank Bültge
* Author URI: http://bueltge.de
* License: GPLv3
*/
remove_filter( 'tiny_mce_before_init', '_mce_set_direction' );
add_filter( 'tiny_mce_before_init', 'fb_tinymce', 9 );
function fb_tinymce( $init ) {
$init['fix_list_elements'] = FALSE;
$init['wpautop'] = FALSE;
$init['remove_linebreaks'] = FALSE;
$init['apply_source_formatting'] = TRUE;
$init['extended_valid_elements'] .= ',pre[*],code[*]';
return $init;
}
@bueltge
Copy link
Author

bueltge commented Jan 25, 2012

alternative via JS; basically a add on for editor.js

jQuery(document).ready( function() {

    // Backup Original, brauchen wir später nochmal
    o_switchEditors = switchEditors;

    /* wir hängen uns mal rein: HTML -> Visual ruft switchEditors.wpautop
    Visual -> HTML ruft pre_wpautop 
    im ersteren Fall sollte folgendes passieren: */

    switchEditors.wpautop = function(content) {

    // RegExp: nimm <pre> Tags bzw. den Inhalt und speichere den zwischen
    pretag = new RegExP...;
    // stattdessen hinterlassen wir irgendein Platzhalter um es später wieder einzufügen
    content = replace...pretag..##Platzhalter##;
    // pretag muss entsprechend in HTML Entities kodiert werden
    // php.js kann helfen oder entsprechender .replace();

    // den eventuellen Restcontent schicken wir durchs Original
    restcontent = o_SwitchEditors.wpautop(content);
    // beides wieder miteinander verheiraten
    content = replace...##Platzhalter##...pretag;
    // und zurück damit
    return content;
    };

    switchEditors.pre_wpautop = function() {
        // das gleiche Spiel, nur andersherum.
    }   

})```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment