Last active
August 19, 2024 02:35
-
-
Save BronsonQuick/3398790 to your computer and use it in GitHub Desktop.
Add custom classes to TinyMCE in WordPress because I hate shortcodes :)
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
<?php | |
/* Add the Style Dropdown Menu to the second row of visual editor buttons | |
* N.B. Make sure you include your custom classes in both your style.css AND editor-style.css :) | |
*/ | |
function pico_themes_mce_buttons( $buttons ) { | |
array_unshift( $buttons, 'styleselect' ); | |
return $buttons; | |
} | |
add_filter( 'mce_buttons_2', 'pico_themes_mce_buttons' ); | |
function pico_themes_mce_before_init( $init_array ) { | |
// Get the current post type and add in the classes | |
$current_post_type = pico_themes_get_current_post_type(); | |
if ( $current_post_type == 'slider' ) { | |
$style_formats = array( | |
// Each array child is a format with it's own settings | |
array( | |
'title' => 'Slider Green', | |
'inline' => 'span', | |
'classes' => 'slidergreen', | |
), | |
); | |
// Insert the array, json encoded, into 'style_formats' | |
$init_array['style_formats'] = json_encode( $style_formats ); | |
return $init_array; | |
} else { | |
$style_formats = array( | |
// Each array child is a format with it's own settings | |
array( | |
'title' => 'Green Button', | |
'inline' => 'span', | |
'classes' => 'reenbutton', | |
), | |
); | |
// Insert the array, json encoded, into 'style_formats' | |
$init_array['style_formats'] = json_encode( $style_formats ); | |
return $init_array; | |
} | |
} | |
add_filter( 'tiny_mce_before_init', 'pico_themes_mce_before_init' ); | |
/** | |
* Gets the current global post type if one is set | |
*/ | |
function pico_themes_get_current_post_type() { | |
global $post, $typenow, $current_screen; | |
if ( $post && $post->post_type ) { | |
$post_type = $post->post_type; | |
} elseif ( $typenow ) { | |
$post_type = $typenow; | |
} elseif ( $current_screen && $current_screen->post_type ) { | |
$post_type = $current_screen->post_type; | |
} elseif ( isset( $_REQUEST['post_type'] ) ) { | |
$post_type = sanitize_key( $_REQUEST['post_type'] ); | |
} else { | |
$post_type = null; | |
} | |
return $post_type; | |
} |
it's good practice to use braces even when you don't need to
I agree for the multi-line blocks (where endif;
is used). For one liners, it's fine and meets the WordPress coding standards. I'm not sure if you were referring to this one, too, but since you said "even when you don't need to" I wanted to point it out. For example:
if ( condition() )
do_it()
else
do_something_else()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're mixing tabs and spaces...
Also, it's good practice to use braces even when you don't need to. You should probably avoid the long-style syntax for blocks (
if
/endif
) when purely in code, they're mainly for use in templates.