Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created September 25, 2014 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatch/a5cbcdaa5ab82a0a30c9 to your computer and use it in GitHub Desktop.
Save jaredatch/a5cbcdaa5ab82a0a30c9 to your computer and use it in GitHub Desktop.
Conditionally change the editor stylesheet loaded in TinyMCE
<?php
/**
* Conditionally change the editor stylesheet loaded in TinyMCE
*
* Few things to note. All this is done inside after_theme_setup (or equivilant).
* Before this is executed we still use are normal add_editor_style( 'css/editor-style.css' );
* to set the default stylesheet. Lastly the example below only loads on edit post
* because loading on new posts wouldn't help since the template hasn't been set. It can
* be loaded on new posts though, reference the codex link below.
*
* @link http://codex.wordpress.org/Function_Reference/add_editor_style
*/
function ja_editor_style_conditional() {
global $post;
// Edit post (pre_get_posts).
if ( stristr( $_SERVER['REQUEST_URI'], 'post.php' ) !== false && is_object( $post ) && 'page' == get_post_type( $post->ID ) ) {
if ( get_page_template_slug( $post->ID ) == 'template-product.php' ) {
add_editor_style( 'css/editor-style-product.css' );
}
}
}
add_action( 'pre_get_posts', 'ja_editor_style_conditional' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment