Skip to content

Instantly share code, notes, and snippets.

@ooksanen
Created March 13, 2020 14:27
Show Gist options
  • Save ooksanen/eacb9aebf87e7cbc7650076c6088e811 to your computer and use it in GitHub Desktop.
Save ooksanen/eacb9aebf87e7cbc7650076c6088e811 to your computer and use it in GitHub Desktop.
WP TinyMCE Shortcode Button Plugin
(function() {
tinymce.PluginManager.add('oo_tmce_button', function( editor, url ) {
editor.addButton( 'oo_tmce_button', {
title: 'Shortcodes',
type: 'menubutton',
icon: 'wp_code',
menu: [
{
text: 'Button',
onclick: function() {
editor.windowManager.open( {
title: 'Button parameters',
body: [{
type: 'textbox',
name: 'text',
label: 'Button text'
},
{
type: 'textbox',
name: 'url',
label: 'URL'
},
{
type: 'textbox',
name: 'classes',
label: 'Button classes separated with space'
}],
onsubmit: function( e ) {
editor.insertContent( '[button text="' + e.data.text + '" url="' + e.data.url + '" classes="' + e.data.classes + '"]');
}
});
}
}
]
});
});
})();
<?php
/**
* Plugin Name: Shortcodes button
* Description: Adds a button for inserting shortcodes in visual editor
* Version: 1.0
* Author: Oskari Oksanen
* Author URI: http://oskarioksanen.fi/
*/
add_action('admin_head', 'oo_add_tmce_button');
function oo_add_tmce_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', 'etapper', 'partners' ) ) )
return;
// check if WYSIWYG is enabled
if ( get_user_option('rich_editing') == 'true') {
add_filter('mce_external_plugins', 'oo_register_tmce_plugin');
add_filter('mce_buttons', 'oo_register_tmce_button');
}
}
function oo_register_tmce_plugin($plugin_array) {
$plugin_array['oo_tmce_button'] = plugins_url( '/tinymce-shortcodes-button.js', __FILE__ );
return $plugin_array;
}
function oo_register_tmce_button($buttons) {
array_push($buttons, 'oo_tmce_button');
return $buttons;
}
// Disable WordPress.org plugin update notifications (NOTE: Plugin needs to be activated!)
add_filter('site_transient_update_plugins', 'remove_update_notification');
function remove_update_notification($value) {
unset($value->response[ plugin_basename(__FILE__) ]);
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment