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.


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).

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.
  • Ooh awesome! Is there also a method for grabbing the id number of the post that the editor is working on too? – Marc P Jan 3 '14 at 20:16
  • Yes, edited to add an example. – helenhousandi Jan 3 '14 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.