0

I am wondering is it posible to define add_editor_style to specific post types?

Currently I am using this function..

// EDITOR STYLE
add_editor_style('editor-style.css');

But I would like have multiple editor styles for various post types.

So guessing it could be like this...

if ( ... ) {

// EDITOR STYLE POSTS & PAGES
add_editor_style('editor-style.css');

} else if ( ... ) {

// EDITOR STYLE CUSTOM POST-TYPE BOOKS
add_editor_style('editor-style-books.css');

}

My post types are page, post, book


Any ideas would be great thanks

1
  • Maybe if used in combination with admin_print_styles-(post-hook) ? Jul 25 '12 at 16:36
4

The first issue is that add_editor_style() is usually called in Theme setup functions that get hooked into after_setup_theme - which is well before the query is set up and the post type determined. So, you'll need to move add_editor_style() to a separate callback, that gets called after the query is setup, but before TinyMCE is initialized. Perhaps tiny_mce_before_init?

<?php
function wpse59547_add_editor_style() {
    global $post;
    $post_type = get_post_type( $post->ID );
    $editor_style = 'editor-style-' . $post_type . '.css';
    add_editor_style( $editor_style );
}
add_action( 'tiny_mce_before_init', 'wpse59547_add_editor_style' );

Then, you just need to create editor-style-post.css, editor-style-book.css, etc.

If you want to default to editor-style.css, use get_post_types( array( 'public' => true, '_builtin' => false ) ) to return an array of custom post types, and use that, e.g.:

$custom_post_types = get_post_types( array( 'public' => true, '_builtin' => false ) );
$editor_style = ( in_array( $post_type, $custom_post_types ) ? 'editor-style-' . $post_type . '.css' : 'editor-style.css' );
2
  • Nice one chip, this was just what I was after. Very big thanks! Josh
    – Joshc
    Jul 25 '12 at 15:48
  • I would rather use editor_stylesheets filter Mar 28 '19 at 17:00

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.