Skip to content

Instantly share code, notes, and snippets.

@michaeldozark
Last active December 12, 2016 16:57
Show Gist options
  • Save michaeldozark/efaaddbfed88e0230b1dbcac5e1433b8 to your computer and use it in GitHub Desktop.
Save michaeldozark/efaaddbfed88e0230b1dbcac5e1433b8 to your computer and use it in GitHub Desktop.
Best way to include parent theme styles when the child theme is already properly enqueued
<?php
/**
* Hook our function. We are using priority 100 to make sure it fires AFTER
* the theme stylesheet is registered/enqueued.
*
* @link https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/
* Description of `wp_enqueue_scripts` hook
*/
add_action( 'wp_enqueue_scripts', 'slimline_enqueue_template_stylesheet', 100 );
/**
* Edit stylesheet version and dependencies to automatically output parent styles.
*
* This allows us to always take advantage of the latest parent theme updates without
* resorting to `@import` rules.
*
* Note that this function assumes that the parent theme uses `style.css` for its theme
* styles. If your parent theme uses a different file or files you will have to modify
* accordingly.
*/
function slimline_enqueue_template_stylesheet() {
/**
* @global WP_Styles $wp_styles The core styles object
* @link https://developer.wordpress.org/reference/classes/wp_styles/
* Description of `WP_Styles` class
*/
global $wp_styles;
/**
* Register template stylesheet
*
* We are getting the version of the parent theme so the parent stylesheet
* version number will automatically update when we update the theme.
*
* @link https://developer.wordpress.org/reference/functions/wp_register_style/
* Description of `wp_register_style` function
* @link https://developer.wordpress.org/reference/functions/trailingslashit/
* Description of `trailingslashit` function
* @link https://developer.wordpress.org/reference/functions/get_template_directory_uri/
* Description of `get_template_directory_uri` function
* @link https://developer.wordpress.org/reference/functions/wp_get_theme/
* Description of `wp_get_theme` function
* @link https://developer.wordpress.org/reference/functions/get_template/
* Description of `get_template` function
* @link https://developer.wordpress.org/reference/classes/wp_theme/
* Description of `WP_Theme` class
*/
wp_register_style( 'theme-parent', trailingslashit( get_template_directory_uri() ) . 'style.css', false, wp_get_theme( get_template() )->get( 'Version' ), 'all' );
/**
* Add template styles as dependency for child theme stylesheet.
*
* This makes sure the parent theme stylesheet will print before the child
* theme stylesheet.
*/
$wp_styles->registered[get_current_theme()]->deps = array( 'theme-parent' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment