Is there a method to apply a unique body class ID to the editor in the same way that body class information is added to the published page?
I am using the add_editor_style() function to render the editor’s styles to reflect the published page. However, there are some pages where the styles break away from the standard page rules, and I would love to be able to include some of those exceptional style rules in the editor.
1 Answer
You can filter the TinyMCE body classes to add or change as needed. It’s a string that’s pre-populated with some things like post type, so the easiest thing to do is append your additional classes (with a preceding space).
<?php
function wpse_128380_tinymce_body_class( $mce ) {
// you could do things here to detect whatever you need
// and use those for the additional classes.
// be safe and use sanitize_html_class or similar if generated.
// example: use the post ID when editing a post
if ( $post = get_post() ) {
$mce['body_class'] .= ' ' . sanitize_html_class( $post->ID );
}
$mce['body_class'] .= ' custom-class another-custom-class etc';
return $mce;
}
add_filter( 'tiny_mce_before_init', 'wpse_128380_tinymce_body_class' );
// if you're using the "teeny" version of the editor,
// it fires the teeny_mce_before_init filter instead.