Last active
June 1, 2016 20:38
-
-
Save stephenbelyea/ec02e8efd9906d5bcf1503c9c97f2f2e to your computer and use it in GitHub Desktop.
Add MCE shortcode button for WP content editor.
This file contains 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
// Register the shortcode. | |
function my_custom_shortcode( $atts ) { | |
extract( shortcode_atts( | |
array( | |
'arg1' => '', | |
'arg2' => '' | |
), $atts ) | |
); | |
$output = "Whatever you need your shortcode to do..."; | |
return $output; | |
} | |
add_shortcode( 'my_shortcode', 'my_custom_shortcode' ); | |
// Add CTA shortcode button. | |
// init process for registering our button | |
add_action('init', 'my_shortcode_button_init'); | |
function my_shortcode_button_init() { | |
//Abort early if the user will never see TinyMCE | |
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') | |
return; | |
//Add a callback to regiser our tinymce plugin | |
add_filter("mce_external_plugins", "my_tinymce_plugin_register"); | |
// Add a callback to add our button to the TinyMCE toolbar | |
add_filter('mce_buttons', 'my_tinymce_button_add'); | |
} | |
//This callback registers our plug-in | |
function my_tinymce_plugin_register($plugin_array) { | |
// Find plugin's JS file in site's theme. | |
$plugin_array['my_shortcode_button_id'] = get_template_directory_uri() . '/js/my-shortcode-button.js'; | |
return $plugin_array; | |
} | |
//This callback adds our button to the toolbar | |
function my_tinymce_button_add($buttons) { | |
//Add the button ID to the $button array | |
$buttons[] = "my_shortcode_button_id"; | |
return $buttons; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment