Add post class to the TinyMCE iframe?

The filter you’re after is tiny_mce_before_init. Using this, we can hook into TinyMCE’s ‘init_array’ and add body classes:

add_filter( 'tiny_mce_before_init', 'wpse_235194_tiny_mce_classes' );

function wpse_235194_tiny_mce_classes( $init_array ){

  global $post;

  if( is_a( $post, 'WP_Post' ) ){
    $init_array['body_class'] .= ' ' . join( ' ', get_post_class( '', $post->ID ) );
  }

  return $init_array;

}

We’re joining the post classes with a space to convert them from an array to a string as required by TinyMCE, and we’re also checking that we do actually have a valid post object, to avoid errors if you’re using TinyMCE elsewhere (such as in widgets or the like).

Leave a Comment