Skip to content

Instantly share code, notes, and snippets.

@nathanieltubb
Last active October 17, 2016 22:30
Show Gist options
  • Save nathanieltubb/be81387d267b27fe8d98d86d649e8784 to your computer and use it in GitHub Desktop.
Save nathanieltubb/be81387d267b27fe8d98d86d649e8784 to your computer and use it in GitHub Desktop.
WordPress TinyMCE Editor Adding Custom Buttons + Enabling Hidden Buttons
<?php
//---------------------------------------------------------------
/**
* Registers an editor stylesheet for the theme.
*/
function custom_theme_add_editor_styles() {
add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'custom_theme_add_editor_styles' );
//---------------------------------------------------------------
/**
* Callback function to insert 'styleselect' into the $buttons array
* The filter mce_buttons_2 refers to the second row of buttons in the editor
* where mce_buttons = first row and mce_buttons_3 = third row.
*/
function custom_enable_mce_styleselect( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
add_filter( 'mce_buttons_2', 'custom_enable_mce_styleselect' );
//---------------------------------------------------------------
/**
* Callback function to filter the MCE settings
*/
function custom_mce_before_init_insert_formats( $init_array ) {
// Define the style_formats array
$style_formats = array(
// Each array child is a format with it's own settings
array(
'title' => 'Shows',
'items' => array(
array(
'title' => 'Title',
'inline' => 'span',
'classes' => 'format-title',
'exact' => true,
),
array(
'title' => 'Subtitle',
'inline' => 'span',
'classes' => 'format-subtitle',
'exact' => true,
),
array(
'title' => 'Presents',
'inline' => 'span',
'classes' => 'format-presents',
'exact' => true,
),
array(
'title' => 'Byline',
'inline' => 'span',
'classes' => 'format-byline',
'exact' => true,
),
array(
'title' => 'Dates',
'inline' => 'span',
'classes' => 'format-dates',
'exact' => true,
)
)
)
);
// Insert the array, JSON ENCODED, into 'style_formats'
$init_array['style_formats'] = json_encode( $style_formats );
return $init_array;
}
// Attach callback to 'tiny_mce_before_init'
add_filter( 'tiny_mce_before_init', 'custom_mce_before_init_insert_formats' );
//---------------------------------------------------------------
/**
* Create an init process for registering our button(s).
*/
add_action('init', 'custom_tinymce_shortcode_button_init');
function custom_tinymce_shortcode_button_init() {
//Add a callback to regiser our tinymce plugin
add_filter( 'mce_external_plugins', 'custom_tinymce_register_plugins' );
// Add a callback to add our button to the TinyMCE toolbar
add_filter( 'mce_buttons', 'custom_tinymce_register_buttons' );
add_filter( 'mce_buttons_3', 'custom_tinymce_register_buttons_3' );
}
/**
* Adding an existing hidden button to the first row.
*/
function custom_tinymce_register_buttons( $buttons ) {
array_push( $buttons, 'table' );
return $buttons;
}
/**
* Adding a custom button to the third row.
*/
function custom_tinymce_register_buttons_3( $buttons ) {
array_push( $buttons, 'custom_flexvideo' );
return $buttons;
}
/**
* Register the JS files that control the operation of the new buttons.
*/
function custom_tinymce_register_plugins( $plugin_array ) {
$plugin_array['table'] = get_stylesheet_directory_uri().'/includes/tinymce/plugins/table/plugin.min.js';
$plugin_array['custom_flexvideo'] = get_stylesheet_directory_uri().'/includes/tinymce/plugins/custom-shortcodes/flexvideo.js';
return $plugin_array;
}
//---------------------------------------------------------------
jQuery(document).ready(function($) {
tinymce.create('tinymce.plugins.custom_flexvideo_plugin', {
init : function(ed, url) {
// Register command for when button is clicked
ed.addCommand('custom_flexvideo_insert_shortcode', function() {
selected = tinyMCE.activeEditor.selection.getContent();
if( selected ){
// If text is selected when button is clicked
// Wrap shortcode around it.
content = '[flexvideo aspect="widescreen" platform="youtube"]'+selected+'[/flexvideo]';
}else{
content = '[flexvideo aspect="widescreen" platform="youtube"]paste_embed_code_here[/flexvideo]';
}
tinymce.execCommand('mceInsertContent', false, content);
});
// Register buttons - trigger above command when clicked
ed.addButton('custom_flexvideo', {
title : 'Insert Flexvideo Shortcode',
cmd : 'custom_flexvideo_insert_shortcode',
image: url + '/icons/icon-flexvideo.svg' });
},
});
// 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('custom_flexvideo', tinymce.plugins.custom_flexvideo_plugin);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment