Last active
October 25, 2018 14:03
-
-
Save vihoangson/6a886db7a90b1c90d560 to your computer and use it in GitHub Desktop.
Make button shortcode for wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery(document).ready(function($) { | |
| tinymce.create('tinymce.plugins.wpse72394_plugin', { | |
| init : function(ed, url) { | |
| // Register command for when button is clicked | |
| ed.addCommand('wpse72394_insert_shortcode', function() { | |
| selected = tinyMCE.activeEditor.selection.getContent(); | |
| if( selected ){ | |
| //If text is selected when button is clicked | |
| //Wrap shortcode around it. | |
| content = '[shortcode]'+selected+'[/shortcode]'; | |
| }else{ | |
| content = '[shortcode]'; | |
| } | |
| tinymce.execCommand('mceInsertContent', false, content); | |
| }); | |
| // Register buttons - trigger above command when clicked | |
| ed.addButton('wpse72394_button', {title : 'Insert shortcode', cmd : 'wpse72394_insert_shortcode', image: '/wp-content/themes/minn-lite/shortcode.png' }); | |
| }, | |
| }); | |
| // Register our TinyMCE plugin | |
| // first parameter is the button ID1 | |
| // second parameter must match the first parameter of the tinymce.create() function above | |
| tinymce.PluginManager.add('wpse72394_button', tinymce.plugins.wpse72394_plugin); | |
| }); |
Author
Hello - this is fantastic and I have it working. Is there a version of this code where I can make an array of buttons, without duplicating all the code, which is what I've done to get two buttons?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview