Created
September 19, 2022 10:27
-
-
Save yaroslav-borodii/4fd10e4e2bf749654df42c5d28e82367 to your computer and use it in GitHub Desktop.
WP: Replace initial format select with custom
This file contains hidden or 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
/* | |
* TinyMCE Custom Styles | |
* https://codex.wordpress.org/TinyMCE_Custom_Styles | |
*/ | |
// Callback function to insert 'styleselect' into the $buttons array | |
function my_mce_buttons( $buttons ) { | |
unset( $buttons[0] ); | |
array_unshift( $buttons, 'styleselect' ); | |
return $buttons; | |
} | |
// Register our callback to the appropriate filter | |
add_filter( 'mce_buttons', 'my_mce_buttons' ); | |
// Callback function to filter the MCE settings | |
function my_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' => 'H1', | |
'block' => 'h1', | |
), | |
array( | |
'title' => 'H3', | |
'block' => 'h2', | |
), | |
array( | |
'title' => 'H3', | |
'block' => 'h3', | |
), | |
array( | |
'title' => 'H4', | |
'block' => 'h4', | |
), | |
array( | |
'title' => 'Paragraph 1 (18px)', | |
'block' => 'p', | |
'classes' => 'p1', | |
), | |
array( | |
'title' => 'Paragraph 2 (16px)', | |
'block' => 'p', | |
'classes' => 'p2', | |
), | |
array( | |
'title' => 'Paragraph 3 (14px)', | |
'block' => 'p', | |
'classes' => 'p3', | |
), | |
); | |
// Insert the array, JSON ENCODED, into 'style_formats' | |
$init_array['style_formats'] = wp_json_encode( $style_formats ); | |
return $init_array; | |
} | |
// Attach callback to 'tiny_mce_before_init' | |
add_filter( 'tiny_mce_before_init', 'my_mce_before_init_insert_formats' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment