Last active
December 12, 2015 08:09
-
-
Save acusti/4742135 to your computer and use it in GitHub Desktop.
WordPress: Add custom TinyMCE button that wraps selection in given tag with a couple extra functions from within your theme’s functions.php
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
<?php | |
/** | |
* Filter TinyMCE wysiwig editor to include custom button | |
* | |
* @since MyTheme 1.0 | |
* | |
* @param $buttons TinyMCE button array | |
* @return $buttons TinyMCE button array | |
*/ | |
function mytheme_mce_buttons_2( $buttons ) { | |
/** | |
* Add in our custom wrapping button | |
*/ | |
$buttons[] = 'wrap'; | |
return $buttons; | |
} | |
add_filter( 'mce_buttons_2', 'mytheme_mce_buttons_2' ); | |
/** | |
* Add custom button handler to TinyMCE settings JS | |
* | |
* @since MyTheme 1.0 | |
* | |
* @param $settings array of TinyMCE settings | |
* @return $settings array of TinyMCE settings | |
*/ | |
function mytheme_tinymce_settings( $settings ) { | |
// Add a setup function which registers our simple button to wrap selection in a <div> with class="customclass" | |
$setup_function = 'function(ed) { | |
ed.addButton("wrap", { | |
title : "Wrap selected content in div.customclass", | |
image : "' . get_template_directory_uri() . '/img/wrap-icon.png", | |
onclick : function() { | |
ed.focus(); | |
ed.selection.setContent("<div class=customclass>" + ed.selection.getContent() + "</div>"); | |
} | |
}); | |
}'; | |
// Strip out new lines (they will break the JS) and add the setup function | |
$settings['setup'] = str_replace( "\n", '', $setup_function ); | |
return $settings; | |
} | |
add_filter( 'tiny_mce_before_init', 'mytheme_tinymce_settings' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment