Created
May 12, 2014 20:17
-
-
Save coderaaron/7ef19e2f1b577cf40479 to your computer and use it in GitHub Desktop.
How to safely add custom style to TinyMCE 4.0 "Formats" dropdown
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 | |
// Add new styles to the TinyMCE "formats" menu dropdown | |
if ( ! function_exists( 'wusm_styles_dropdown' ) ) { | |
function wusm_styles_dropdown( $settings ) { | |
// Create array of new styles | |
// array of arrays, see example here: http://codex.wordpress.org/TinyMCE_Custom_Styles | |
$new_styles = array( | |
array( | |
'title' => 'Custom Styles', | |
'items' => array( | |
array( | |
'title' => 'Main content callout', | |
'block' => 'div', | |
'classes' => 'callout', | |
'wrapper' => 'true' | |
) | |
), | |
), | |
); | |
// Merge old & new styles | |
// Setting this to true will inclue the style that are included as buttons | |
// by default by WordPress | |
$settings['style_formats_merge'] = false; | |
// Add new styles | |
if( ! isset( $settings['style_formats'] ) ) { | |
$settings['style_formats'] = json_encode( $new_styles ); | |
} else { | |
$settings['style_formats'] = json_encode( array_merge( json_decode( $settings['style_formats'] ), $new_styles ) ); | |
} | |
// Return New Settings | |
return $settings; | |
} | |
} | |
add_filter( 'tiny_mce_before_init', 'wusm_styles_dropdown' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment