Created
August 15, 2023 08:51
-
-
Save jpneey/0cd67d5c73a9bcf4f1e6912e34bf2d04 to your computer and use it in GitHub Desktop.
Utility wordpress class to extend tiny MCE
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 | |
/** | |
* Utility class to extend tiny mce | |
*/ | |
class EchoTinyMCE { | |
public function __construct() { | |
// Enable style dropdown menu | |
add_filter( 'mce_buttons', array( $this, 'enable_styleselect' ) ); | |
// Add new styles to the TinyMCE "formats" menu dropdown. | |
add_filter( 'tiny_mce_before_init', array( $this, 'add_formats' ) ); | |
// Add TinyMCE editor stylesheet | |
add_action( 'after_setup_theme', array( $this, 'load_editor_stylsheet' ) ); | |
// Add TinyMCE javascript | |
add_action( 'mce_external_plugins', array( $this, 'load_editor_script' ) ); | |
add_action( 'mce_buttons', array( $this, 'register_editor_script' ) ); | |
add_filter( 'tiny_mce_before_init', array( $this, 'add_editor_class') ); | |
} | |
/** | |
* Enable styleselect | |
* @param Array $buttons array of enabled TinyMCE buttons | |
* @return Array Updated array of enabled TinyMCE buttons | |
*/ | |
public function enable_styleselect( Array $buttons ) { | |
array_push( $buttons, 'styleselect' ); | |
return $buttons; | |
} | |
/** | |
* Add "formats" menu drowdown on TinyMCE. | |
* This adds a theme button and highlight options | |
* @param Array $settings array of TinyMCE settings | |
*/ | |
public function add_formats( Array $settings ) { | |
$styles = array( | |
array( | |
"title" => "Buttons", | |
"items" => array( | |
array( | |
"title" => "Primary button", | |
"selector" => 'a', | |
"classes" => "button" | |
), | |
array( | |
"title" => "Secondary button", | |
"selector" => 'a', | |
"classes" => "button-alt" | |
) | |
) | |
), | |
array( | |
"title" => "Lists", | |
"items" => array( | |
array( | |
"title" => "Two column", | |
"selector" => 'ul', | |
"classes" => "ul-two-col" | |
), | |
array( | |
"title" => "Large links", | |
"selector" => 'ul', | |
"classes" => "ul-large-links" | |
) | |
) | |
) | |
); | |
// Merge with existing settings | |
$settings['style_formats_merge'] = true; | |
$settings['style_formats'] = json_encode( $styles ); | |
return $settings; | |
} | |
public function load_editor_stylsheet() { | |
add_editor_style( trailingslashit( get_template_directory_uri() ) . 'assets/css/reset.css?v=' . _S_VERSION ); | |
add_editor_style( trailingslashit( get_template_directory_uri() ) . 'assets/css/bootstrap.min.css?v=' . _S_VERSION ); | |
add_editor_style( trailingslashit( get_template_directory_uri() ) . 'assets/css/main.css?v=' . _S_VERSION ); | |
add_editor_style( trailingslashit( get_template_directory_uri() ) . 'assets/css/tiny-mce.css?v=' . _S_VERSION ); | |
} | |
/** | |
* Load additional plugin(s) for TinyMCE | |
* @param Array $plugin_array array of available plugins | |
* @return Array $plugin_array modified array of available plugins | |
*/ | |
public function load_editor_script( Array $plugin_array ) { | |
$plugin_array['echo_mce_button'] = get_template_directory_uri() . '/assets/js/mce-button.js?v=10'; | |
return $plugin_array; | |
} | |
/** | |
* Register additional button(s) for TinyMCE | |
* @param Array $buttons array of registered buttons | |
* @return Array $buttons modified array of registered buttons | |
*/ | |
public function register_editor_script( Array $buttons ) { | |
array_push( $buttons, 'echo_mce_button' ); | |
return $buttons; | |
} | |
public function add_editor_class( $mce ) { | |
if ( $post = get_post() ) { | |
$mce['body_class'] .= ' wysiwyg'; | |
} | |
$mce['body_class'] .= ' custom-class another-custom-class etc'; | |
return $mce; | |
} | |
} | |
new EchoTinyMCE(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment