Add unique class or ID information to tinyMCE

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.

Leave a Comment