Skip to content

Instantly share code, notes, and snippets.

@elearningplugins
Created June 20, 2023 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elearningplugins/e069f8a4495d37f2379c110c8403620f to your computer and use it in GitHub Desktop.
Save elearningplugins/e069f8a4495d37f2379c110c8403620f to your computer and use it in GitHub Desktop.
Adds a custom button to Oxygen Builder's TinyMCE editor for "rich text"
<?php
/**
* Plugin Name: My Custom Button
* Description: This is a plugin that adds a custom button to the Oxygen Builder's TinyMCE editor.
* Version: 1.0.0
* Author: Your Name
*/
function custom_button_plugin() {
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
return;
}
if (get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'add_custom_tinymce_plugin');
add_filter('mce_buttons', 'register_custom_button');
}
}
add_action('init', 'custom_button_plugin');
function add_custom_tinymce_plugin($plugin_array) {
$plugin_array['custom_button'] = plugin_dir_url(__FILE__) . 'my-custom-button.php?load_js=1';
return $plugin_array;
}
function register_custom_button($buttons) {
array_push($buttons, "|", "custom_button");
return $buttons;
}
function load_js() {
if(isset($_GET['load_js'])) {
header('Content-Type: application/javascript');
echo "(function() {
tinymce.create('tinymce.plugins.custom_button', {
init: function(editor, url) {
editor.addButton('custom_button', {
title: 'My Custom Button',
image: url + '/button-icon.png',
onclick: function() {
alert('You clicked my custom button!');
}
});
},
createControl: function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('custom_button', tinymce.plugins.custom_button);
})();";
exit();
}
}
add_action('init', 'load_js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment