kobial8

Shortcode Button To Post Editor/TinyMCE Editor

Dec 2nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Step 1: In your theme's inc folder create a new file named as shortcode-list.php with the following codes:
  2. <?php
  3. // Hooks your functions into the correct filters
  4. function my_add_mce_button() {
  5.     // check user permissions
  6.     if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
  7.         return;
  8.     }
  9.     // check if WYSIWYG is enabled
  10.     if ( 'true' == get_user_option( 'rich_editing' ) ) {
  11.         add_filter( 'mce_external_plugins', 'my_add_tinymce_plugin' );
  12.         add_filter( 'mce_buttons', 'my_register_mce_button' );
  13.     }
  14. }
  15. add_action('admin_head', 'my_add_mce_button');
  16.  
  17. // Declare script for new button
  18. function my_add_tinymce_plugin( $plugin_array ) {
  19.     $plugin_array['my_mce_button'] = get_template_directory_uri() .'/js/mce-button.js';
  20.     return $plugin_array;
  21. }
  22.  
  23. // Register new button in the editor
  24. function my_register_mce_button( $buttons ) {
  25.     array_push( $buttons, 'my_mce_button' );
  26.     return $buttons;
  27. }
  28. ?>
  29.  
  30. Step 2: Then require this file with functions.php file as:
  31. require get_template_directory() . '/inc/shortcode-list.php';
  32.  
  33. Step 3: Create a new JS file in your theme's js folder as mce-button.js with these following codes:
  34.  
  35. (function() {
  36.     tinymce.PluginManager.add('my_mce_button', function( editor, url ) {
  37.         editor.addButton( 'my_mce_button', {
  38.             text: 'Shortcodes',
  39.             icon: false,
  40.             type: 'menubutton',
  41.             menu: [
  42.                 {
  43.                     text: 'Video',
  44.                        onclick: function() {
  45.                             editor.windowManager.open( {
  46.                                 title: 'Insert Video URL',
  47.                                 body: [
  48.                                     {
  49.                                         type: 'textbox',
  50.                                         name: 'sectionTitle',
  51.                                         label: 'Video Link',
  52.                                         value: 'Put video link including https'
  53.                                     }                                  
  54.                                 ],
  55.                                 onsubmit: function( e ) {
  56.                                     editor.insertContent( '[custom_video link="' + e.data.sectionTitle + '" ]');
  57.                                 }
  58.                             });
  59.                         }
  60.                 },
  61.                 {
  62.                     text: 'Main Slider',
  63.                        onclick: function() {
  64.                             editor.windowManager.open( {
  65.                                 title: 'Insert Slider Section',
  66.                                 body: [
  67.                                     {
  68.                                         type: 'textbox',
  69.                                         name: 'sectionTitle',
  70.                                         label: 'Section Title',
  71.                                         value: 'If you do not need title for your slider, keep this box blank'
  72.                                     },
  73.                                     {
  74.                                         type: 'textbox',
  75.                                         name: 'sectionDescription',
  76.                                         label: 'Section Description',
  77.                                         value: 'If you do not need description for your slider, keep this box blank',
  78.                                         multiline: true,
  79.                                         minWidth: 300,
  80.                                         minHeight: 100
  81.                                     },
  82.                                     {
  83.                                         type: 'textbox',
  84.                                         name: 'categoryName',
  85.                                         label: 'Section Title',
  86.                                         value: 'If you do not need title for your slider, keep this box blank'
  87.                                     }                                    
  88.                                 ],
  89.                                 onsubmit: function( e ) {
  90.                                     editor.insertContent( '[slider title="' + e.data.sectionTitle + '" des="' + e.data.sectionDescription + '" category="' + e.data.categoryName + '" ]');
  91.                                 }
  92.                             });
  93.                         }
  94.                 },             
  95.                 {
  96.                     text: 'Portfolio',
  97.                        onclick: function() {
  98.                             editor.windowManager.open( {
  99.                                 title: 'Insert Portfolio Section',
  100.                                 body: [
  101.                                     {
  102.                                         type: 'textbox',
  103.                                         name: 'sectionTitle',
  104.                                         label: 'Section Title',
  105.                                         value: 'If you do not need title for your slider, keep this box blank'
  106.                                     },
  107.                                     {
  108.                                         type: 'textbox',
  109.                                         name: 'sectionDescription',
  110.                                         label: 'Section Description',
  111.                                         value: 'If you do not need description for your slider, keep this box blank',
  112.                                         multiline: true,
  113.                                         minWidth: 300,
  114.                                         minHeight: 100
  115.                                     },
  116.                                     {
  117.                                         type: 'textbox',
  118.                                         name: 'categoryName',
  119.                                         label: 'Section Title',
  120.                                         value: 'If you do not need title for your slider, keep this box blank'
  121.                                     }                                    
  122.                                 ],
  123.                                 onsubmit: function( e ) {
  124.                                     editor.insertContent( '[portfolio title="' + e.data.sectionTitle + '" des="' + e.data.sectionDescription + '" category="' + e.data.categoryName + '" ]');
  125.                                 }
  126.                             });
  127.                         }
  128.                 }
  129.                
  130.             ]
  131.         });
  132.     });
  133. })();
Add Comment
Please, Sign In to add comment