Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active May 18, 2020 21:32
Show Gist options
  • Save smeric/f1135061ae2e0043804406f1ebe6414a to your computer and use it in GitHub Desktop.
Save smeric/f1135061ae2e0043804406f1ebe6414a to your computer and use it in GitHub Desktop.
Add a post-type specific editor stylesheet to each post edit screen in WordPress back-end. See https://sebastien-meric.com/wordpress-gutenberg-feuille-de-style-specifique-type-de-contenu-post-type/ (in french).
<?php
/**
* Registers a specific editor stylesheet for the current post type.
*
* @see https://developer.wordpress.org/reference/functions/add_editor_style/
*/
function my_theme_add_editor_styles() {
global $pagenow, $current_screen;
// Current post-type
$post_type = $current_screen->post_type ? $current_screen->post_type : '';
// Custom post types
$custom_post_types = get_post_types( array( 'public' => true, '_builtin' => false ) );
// Default post and page post types
$custom_post_types['post'] = 'post';
$custom_post_types['page'] = 'page';
if ( in_array( $post_type, $custom_post_types )
&& ( 'post-new.php' === $pagenow || 'post.php' === $pagenow )
&& file_exists( get_stylesheet_directory() . '/editor-style-' . $post_type . '.css' )
) {
add_editor_style( get_stylesheet_directory_uri() . '/editor-style-' . $post_type . '.css' );
}
}
add_action( 'current_screen', 'my_theme_add_editor_styles' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment