Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active August 10, 2021 13:04
Show Gist options
  • Save smeric/16080e756055fb23fd0d59277f48b6d2 to your computer and use it in GitHub Desktop.
Save smeric/16080e756055fb23fd0d59277f48b6d2 to your computer and use it in GitHub Desktop.
This is how to assign a specific stylesheet to a specific post type post id on WordPress Gutenberg edit screen. See https://sebastien-meric.com/appliquer-gutenberg-feuille-de-style-post-id/ (in french).
<?php
/**
* Registers a specific editor stylesheet for a specific post id.
*/
function actusoins_add_editor_styles_by_post_id() {
global $pagenow;
// Are we on a post edit page ?
if ( isset( $pagenow ) && 'post.php' === $pagenow ) {
$post_id = $_GET['post'];
// Homepage
if ( get_option( 'page_on_front' ) == $post_id && file_exists( get_stylesheet_directory() . '/assets/css/editor-style-home.css' ) ) {
add_editor_style( 'assets/css/editor-style-home.css' );
}
// Blog homepage
if ( get_option( 'page_for_posts' ) == $post_id && file_exists( get_stylesheet_directory() . '/assets/css/editor-style-blog.css' ) ) {
add_editor_style( 'assets/css/editor-style-blog.css' );
}
}
}
add_action( 'admin_init', 'my_theme_add_editor_styles_by_post_id' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment