editor – Wordpres Tinymce custom button (Gavick guide)

Question

I’m trying to adda custom button to my editor following this guide

Can’t get it to work. I’ve added the following code to my funcitons.php

  add_action('admin_head', 'gavickpro_add_my_tc_button');

function gavickpro_add_my_tc_button() {
    global $typenow;
    // check user permissions
    if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
    return;
    }
    // verify the post type
    if( ! in_array( $typenow, array( 'post', 'page' ) ) )
        return;
    // check if WYSIWYG is enabled
    if ( get_user_option('rich_editing') == 'true') {
        add_filter("mce_external_plugins", "gavickpro_add_tinymce_plugin");
        add_filter('mce_buttons', 'gavickpro_register_my_tc_button');
    }
}

function gavickpro_add_tinymce_plugin($plugin_array) {
    $plugin_array['gavickpro_tc_button'] = plugins_url( '/kevin/editor_plugin.js', __FILE__ ); // CHANGE THE BUTTON SCRIPT HERE
    return $plugin_array;
}

function gavickpro_register_my_tc_button($buttons) {
   array_push($buttons, "gavickpro_tc_button");
   return $buttons;
}

and the following js file in my plugins folder kevin/editor_plugin.js

(function() {
    tinymce.PluginManager.add('gavickpro_tc_button', function( editor, url ) {
        editor.addButton( 'gavickpro_tc_button', {
            title: 'My test button',
            icon: 'wp_code',
       

 onclick: function() {
            editor.insertContent('Hello World!');
        }
    });
});

})();

0
Kevin 2 years 2021-11-22T04:29:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse