From 9c3d076240c3616b978e9ca72394376329de24fe Mon Sep 17 00:00:00 2001 From: theaminuldev Date: Sat, 6 Sep 2025 01:53:26 +0600 Subject: [PATCH 1/2] Automatically enqueue style.css when creating a blank theme --- assets/boilerplate/functions.php | 39 ++++++++++++++++++++++++++ includes/create-theme/theme-create.php | 7 +++++ 2 files changed, 46 insertions(+) create mode 100644 assets/boilerplate/functions.php diff --git a/assets/boilerplate/functions.php b/assets/boilerplate/functions.php new file mode 100644 index 00000000..553c90ac --- /dev/null +++ b/assets/boilerplate/functions.php @@ -0,0 +1,39 @@ +get( 'Version' ) + ); + + } else { + error_log( 'Stylesheet not found: ' . $stylesheet_path ); + } + } +); diff --git a/includes/create-theme/theme-create.php b/includes/create-theme/theme-create.php index 736f0b1b..d467c5d8 100644 --- a/includes/create-theme/theme-create.php +++ b/includes/create-theme/theme-create.php @@ -124,6 +124,13 @@ public static function create_blank_theme( $theme, $screenshot ) { } } + // Ensure functions.php is present to enqueue style.css + $functions_src = $source . DIRECTORY_SEPARATOR . 'functions.php'; + $functions_dest = $blank_theme_path . DIRECTORY_SEPARATOR . 'functions.php'; + if ( file_exists( $functions_src ) ) { + copy( $functions_src, $functions_dest ); + } + // Overwrite default screenshot if one is provided. if ( static::is_valid_screenshot( $screenshot ) ) { file_put_contents( From 2043c1ea41bcf4b106b5c310816190c96e7706ae Mon Sep 17 00:00:00 2001 From: Md Aminul Islam <56115259+theaminuli@users.noreply.github.com> Date: Sun, 7 Sep 2025 19:07:20 +0600 Subject: [PATCH 2/2] Refactor theme stylesheet enqueue functions --- assets/boilerplate/functions.php | 86 +++++++++++++++++++------------- 1 file changed, 51 insertions(+), 35 deletions(-) diff --git a/assets/boilerplate/functions.php b/assets/boilerplate/functions.php index 553c90ac..ec2bcf29 100644 --- a/assets/boilerplate/functions.php +++ b/assets/boilerplate/functions.php @@ -1,39 +1,55 @@ get( 'Version' ) - ); +if ( ! function_exists( 'theme_enqueue_style' ) ) { + /** + * Enqueue the theme's main stylesheet on the frontend. + * + * This function checks if the `style.css` file exists in the theme directory + * before attempting to enqueue it. It ensures that missing files do not + * generate PHP warnings and also applies the theme version for cache busting. + * + * Usage: Hooked into 'wp_enqueue_scripts'. + * + * @since 1.0.0 + * + * @see https://developer.wordpress.org/reference/functions/wp_enqueue_style/ + * @see https://developer.wordpress.org/reference/functions/get_stylesheet_directory_uri/ + * @see https://developer.wordpress.org/reference/functions/wp_get_theme/ + * + * @return void + */ + function theme_enqueue_style() { + wp_enqueue_style( + 'theme-style', + get_template_directory_uri() . '/style.css', + array(), + wp_get_theme()->get( 'Version' ) + ); + } + add_action( 'wp_enqueue_scripts', 'theme_enqueue_style' ); +} - } else { - error_log( 'Stylesheet not found: ' . $stylesheet_path ); - } +if ( ! function_exists( 'theme_enqueue_editor_style' ) ) { + /** + * Enqueue the theme's main stylesheet in the block editor. + * + * This function enqueues the `style.css` file in the block editor to ensure + * that the editor styles match the frontend styles. It uses the + * `add_editor_style` function which is specifically designed for this purpose. + * + * Usage: Hooked into 'after_setup_theme'. + * + * @since 1.0.0 + * + * @see https://developer.wordpress.org/reference/functions/add_editor_style/ + * + * @return void + */ + function theme_enqueue_editor_style() { + add_editor_style( 'style.css' ); } -); + add_action( 'after_setup_theme', 'theme_enqueue_editor_style' ); +}