How do I remove keyboard shortcuts in tinyMCE 4?

I was wondering if anyone can provide me with a solution for removing keyboard shortcuts from tinyMCE in Wordpress 4.0? Since I am using an external text editor, I want to disable ALT + SHIFT + M (opens the media gallery) and ALT + F (opens full screen and users cannot add open brackets)

Any help would be appreciated

+3


source to share


1 answer


Here's a neat solution ( original answer here ), just add this to your functions.php file :

<?php
    add_action( 'wp_tiny_mce_init', function () {
?>
    <script>
        function wp_disable_shortcuts_tiny_mce_init(editor) {
            editor.on('init', function () {
                    this.addShortcut('alt+ctrl+f', '', function () {}); //altgr is alt+ctrl
                    this.addShortcut('alt+ctrl+g', '', function () {}); //just in case...
                    this.addShortcut('alt+shift+m', '', function () {});

                    //you could add a for loop to disable multiple shortcuts as in OP answer
                    // var ctrls = [ 'a', 'b', 'c', 'd' ];
                    // for( var i = 0; i < ctrls.length; i++ ){
                    //     this.addShortcut('ctrl+' + ctrls[i], '', function () {});
                    // }
                });
            }
        </script>
<?php
    });
    function wp_disable_shortcuts_tiny_mce_before_init( $mceInit ) {
        $mceInit['setup'] = 'wp_disable_shortcuts_tiny_mce_init';
        return $mceInit;
    }
    add_filter( 'tiny_mce_before_init', 'wp_disable_shortcuts_tiny_mce_before_init' );
?>

      

And here is a short description of the functions and filters used.

This filter gives developers access to an array of TinyMCE settings,

add_filter( 'tiny_mce_before_init', 'wp_disable_shortcuts_tiny_mce_before_init' );

      

Here we can connect our js function to the TinyMCE setup using the following command:



function wp_disable_shortcuts_tiny_mce_before_init( $mceInit ) {
    $mceInit['setup'] = 'wp_disable_shortcuts_tiny_mce_init';
    return $mceInit;
}

      

And finally, this action

add_action( 'wp_tiny_mce_init', function(){ ... });

      

Fires after loading tinymce.js, but before instantiating the TinyMCE editor. Here we create our javascript function that will disable the editor shortcuts in init, replacing them with no-op functions.

Hope it helps.

+2


source







All Articles