Skip to content

Instantly share code, notes, and snippets.

@w3guy
Forked from rxnlabs/php-wp-add-tinymce-button.php
Last active January 11, 2019 10:44
Show Gist options
  • Save w3guy/446c005c2fd5912fa65a to your computer and use it in GitHub Desktop.
Save w3guy/446c005c2fd5912fa65a to your computer and use it in GitHub Desktop.
<?php
/*Add this code to your functions.php file of current theme OR plugin file if you're making a plugin*/
//add the button to the tinymce editor
add_action('media_buttons_context','add_my_tinymce_media_button');
function add_my_tinymce_media_button($context){
return $context.=__("
<a href=\"#TB_inline?width=480&inlineId=my_shortcode_popup&width=640&height=513\" class=\"button thickbox\" id=\"my_shortcode_popup_button\" title=\"Add My Shortcode\">Add My Shortcode</a>");
}
add_action('admin_footer','my_shortcode_media_button_popup');
//Generate inline content for the popup window when the "my shortcode" button is clicked
function my_shortcode_media_button_popup(){?>
<div id="my_shortcode_popup" style="display:none;">
<--".wrap" class div is needed to make thickbox content look good-->
<div class="wrap">
<div>
<h2>Insert My Shortcode</h2>
<div class="my_shortcode_add">
<input type="text" id="id_of_textbox_user_typed_in"><button class="button-primary" id="id_of_button_clicked">Add Shortcode</button>
</div>
</div>
</div>
</div>
<?php
}
//javascript code needed to make shortcode appear in TinyMCE edtor
add_action('admin_footer','my_shortcode_add_shortcode_to_editor');
function my_shortcode_add_shortcode_to_editor(){?>
<script>
jQuery('#id_of_button_clicked ').on('click',function(){
var user_content = jQuery('#id_of_textbox_user_typed_in').val();
var shortcode = '[my_shortcode attributes="'+user_content+'"/]';
if( !tinyMCE.activeEditor || tinyMCE.activeEditor.isHidden()) {
jQuery('textarea#content').val(shortcode);
} else {
tinyMCE.execCommand('mceInsertContent', false, shortcode);
}
//close the thickbox after adding shortcode to editor
self.parent.tb_remove();
});
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment