Skip to content

Instantly share code, notes, and snippets.

@lilumi
Last active July 25, 2019 15:53
Show Gist options
  • Save lilumi/911221cc1a8f1e9d5743a20919127b4d to your computer and use it in GitHub Desktop.
Save lilumi/911221cc1a8f1e9d5743a20919127b4d to your computer and use it in GitHub Desktop.
Add button to TinyMCE
<?php
/**
* Registers an editor stylesheet for the theme.
*/
function lm_theme_add_editor_styles() {
add_editor_style( get_stylesheet_directory_uri().'/editor-style.css?ver='.filemtime(get_stylesheet_directory().'/editor-style.css')); //by default editor-style.css
}
add_action( 'admin_init', 'lm_theme_add_editor_styles' );
/**
* Add "Styles" drop-down to the second row of buttons
*/
add_filter( 'mce_buttons_2', 'tuts_mce_editor_buttons' );
function tuts_mce_editor_buttons( $buttons ) {
array_unshift( $buttons, 'styleselect' );
return $buttons;
}
// Callback function to filter the MCE settings
function lm_mce_before_init_insert_formats( $settings ) {
// Define the style_formats array
$style_formats = array(
// Each array child is a format with it's own settings
array(
'title' => 'Big Title',
'block' => 'h3',
'classes' => 'btitle',
),
array(
'title' => 'Small Title',
'block' => 'p',
'classes' => 'sline',
),
array(
'title' => 'Video Link',
'selector' => 'a',
'exact' => true,
'classes' => 'svideo fancyboxforwp',
),
array(
'title' => 'Button',
'selector' => 'a',
'exact' => true,
'classes' => 'btn',
),
);
if ( isset( $settings['style_formats'] ) ) {
$orig_style_formats = json_decode( $settings['style_formats'], true );
$style_formats = array_merge( $orig_style_formats, $style_formats );
}
$settings['style_formats'] = json_encode( $style_formats );
return $settings;
}
// Attach callback to 'tiny_mce_before_init'
add_filter( 'tiny_mce_before_init', 'lm_mce_before_init_insert_formats' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment