Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
Maybe if used in combination with admin_print_styles-(post-hook) ? –  internetross Jul 25 '12 at 16:36

1 Answer 1

up vote 3 down vote accepted

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' );
share|improve this answer
    
Nice one chip, this was just what I was after. Very big thanks! Josh –  Joshc Jul 25 '12 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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