Support

Account

Home Forums ACF PRO ACF & HTML Editor Syntax Highlighter Reply To: ACF & HTML Editor Syntax Highlighter

  • I had to hack my own solution to get the syntax highlighting to show up for the wysiwyg editor. It would be nice if it was built in. Simply insert this code in your functions.php file to get code mirrors’ syntax highlighting:

    /* ADD CODE MIRROR CSS */
    function admin_style() {
    wp_enqueue_style(‘admin-styles’, ‘https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.css’);
    }
    add_action(‘admin_enqueue_scripts’, ‘admin_style’);

    /* ENQEUE CODE MIRROR JS */
    function my_enqueue() {
    wp_enqueue_script(‘code_mirror_script1’, ‘https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/codemirror.js’);
    wp_enqueue_script(‘code_mirror_script2’, ‘https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.48.4/mode/xml/xml.js’);
    }
    add_action(‘admin_enqueue_scripts’, ‘my_enqueue’);

    /* ADD CODE MIRROR ADDITIONAL STYLES */
    add_action(‘admin_head’, ‘custom_css’);
    function custom_css() {
    echo ‘
    <style>
    .tmce-active .mce-tinymce{display:block !important;visibility:visible !important;}
    .html-active .mce-tinymce{display:none !important;visibility:hidden !important;}
    .tmce-active .CodeMirror{display:none;}
    </style>
    <script>
    jQuery(document).ready(function($){
    function rundCodeMirror(){
    var areas = document.getElementsByClassName(“wp-editor-area”);
    for(var i = 0; i < areas.length; i++) {
    CodeMirror.fromTextArea(areas.item(i), {
    mode : “xml”,
    htmlMode: true,
    lineNumbers: true
    });
    }
    }
    setTimeout(rundCodeMirror, 1000);
    });
    </script>
    ‘;
    }