Skip to content

Instantly share code, notes, and snippets.

@ibrahimkholil
Created October 23, 2016 16:02
Show Gist options
  • Save ibrahimkholil/d466474398197ca4ec145268436f7d35 to your computer and use it in GitHub Desktop.
Save ibrahimkholil/d466474398197ca4ec145268436f7d35 to your computer and use it in GitHub Desktop.
Shortcode menu in post tinymce
http://www.wpexplorer.com/wordpress-tinymce-tweaks/
1. first setup this ex(tinymce.php):
<?php
// Hooks your functions into the correct filters
function corlatne_tinymce_button() {
// check user permissions
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
// check if WYSIWYG is enabled
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_external_plugins', 'corlate_theme_tinymce_plugin' );
add_filter( 'mce_buttons', 'corlate_theme_reg_btn' );
}
}
add_action('admin_head', 'corlatne_tinymce_button');
// Declare script for new button
function corlate_theme_tinymce_plugin( $plugin_array ) {
$plugin_array['corlate_buttons'] = get_template_directory_uri() .'/js/shortcode.js';
return $plugin_array;
}
// Register new button in the editor
function corlate_theme_reg_btn( $buttons ) {
array_push( $buttons, 'corlate_buttons' );
return $buttons;
}
2. include js file (tinymce.js):
(function() {
tinymce.PluginManager.add('my_mce_button', function( editor, url ) {
editor.addButton( 'my_mce_button', {
text: 'Sample Dropdown',
icon: false,
type: 'menubutton',
menu: [
{
text: 'Item 1',
menu: [
{
text: 'Pop-Up',
onclick: function() {
editor.windowManager.open( {
title: 'Insert Random Shortcode',
body: [
{
type: 'textbox',
name: 'textboxName',
label: 'Text Box',
value: '30'
},
{
type: 'textbox',
name: 'multilineName',
label: 'Multiline Text Box',
value: 'You can say a lot of stuff in here',
multiline: true,
minWidth: 300,
minHeight: 100
},
{
type: 'listbox',
name: 'listboxName',
label: 'List Box',
'values': [
{text: 'Option 1', value: '1'},
{text: 'Option 2', value: '2'},
{text: 'Option 3', value: '3'}
]
}
],
onsubmit: function( e ) {
editor.insertContent( '[random_shortcode textbox="' + e.data.textboxName + '" multiline="' + e.data.multilineName + '" listbox="' + e.data.listboxName + '"]');
}
});
}
}
]
}
]
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment