Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created December 9, 2011 12:04
Show Gist options
  • Save GaryJones/1451279 to your computer and use it in GitHub Desktop.
Save GaryJones/1451279 to your computer and use it in GitHub Desktop.
Adding TinyMCE style selector, that plays nice with others. Checks if a style selector has already been added to row 2, and also allows for styles that might already exist from other code / theme / plugin. Also internationalizes displayed labels.
<?php
add_filter( 'mce_buttons_2', 'wps_mce_buttons_2' );
/**
* Show the style dropdown on the second row of the editor toolbar.
*
* This code also adds the font family and font size dropdowns too, along with a horizontal rule button.
*
* @param array $buttons Exising buttons
* @return array Amended buttons
*/
function wps_mce_buttons_2( array $buttons ) {
$additional_buttons = array( 'styleselect', 'fontselect', 'fontsizeselect', 'hr' );
return array_unique( array_merge( $buttons, $additional_buttons ) );
}
add_filter( 'tiny_mce_before_init', 'wps_mce_before_init' );
/**
* Add column entries to the style dropdown.
*
* 'text-domain' should be replaced with your theme or plugin text domain for
* translations.
*
* @param array $settings Existing settings for all toolbar items
* @return array Amended settings
*/
function wps_mce_before_init( array $settings ) {
$style_formats = array(
array( 'title' => __( 'Columns', 'text-domain' ), ),
array(
'title' => __( 'First Half', 'text-domain' ),
'block' => 'div',
'classes' => 'one-half first',
),
array(
'title' => __( 'Half', 'text-domain' ),
'block' => 'div',
'classes' => 'one-half',
),
array(
'title' => __( 'First Third', 'text-domain' ),
'block' => 'div',
'classes' => 'one-third first',
),
array(
'title' => __( 'Third', 'text-domain' ),
'block' => 'div',
'classes' => 'one-third',
),
array(
'title' => __( 'First Quarter', 'text-domain' ),
'block' => 'div',
'classes' => 'one-fourth first',
),
array(
'title' => _( 'Quarter', 'text-domain' ),
'block' => 'div',
'classes' => 'one-fourth',
),
array(
'title' => __( 'First Fifth', 'text-domain' ),
'block' => 'div',
'classes' => 'one-fifth first',
),
array(
'title' => __( 'Fifth', 'text-domain' ),
'block' => 'div',
'classes' => 'one-fifth',
),
array(
'title' => __( 'First Sixth', 'text-domain' ),
'block' => 'div',
'classes' => 'one-sixth first',
),
array(
'title' => __( 'Sixth', 'text-domain' ),
'block' => 'div',
'classes' => 'one-sixth',
),
);
// Check if there are some styles already
if ( isset( $settings['style_formats'] ) ) {
// Decode any existing style formats
$existing_style_formats = json_decode( $settings['style_formats'] );
// Merge our new formats with any existing formats and re-encode
$settings['style_formats'] = json_encode( array_merge( (array) $existing_style_formats, $style_formats ) );
} else {
$settings['style_formats'] = json_encode( $style_formats );
}
return $settings;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment