Created
December 1, 2021 17:05
-
-
Save Garconis/4fc4e917a44a543b2589012a96baab72 to your computer and use it in GitHub Desktop.
WordPress | Divi child theme enqueue for use in functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // goes in functions.php | |
/* Enqueue necessary CSS and JS files for Child Theme */ | |
function fs_theme_enqueue_stuff() { | |
// Divi assigns its style.css with this handle | |
$parent_handle = 'divi-style'; | |
// Get the current child theme data | |
$current_theme = wp_get_theme(); | |
// get the parent version number of the current child theme | |
$parent_version = $current_theme->parent()->get('Version'); | |
// get the version number of the current child theme | |
$child_version = $current_theme->get('Version'); | |
// we check file date of child stylesheet and script, so we can append to version number string (for cache busting) | |
$style_cache_buster = date("YmdHis", filemtime( get_stylesheet_directory() . '/style.css')); | |
$script_cache_buster = date("YmdHis", filemtime( get_stylesheet_directory() . '/script.js')); | |
// first we pull in the parent theme styles that it needs | |
wp_enqueue_style( $parent_handle, get_template_directory_uri() . '/style.css', array(), $parent_version ); | |
// then we get the child theme style.css file, which is dependent on the parent theme style, then append string of child version and file date | |
wp_enqueue_style( 'fs-child-style', get_stylesheet_uri(), array( $parent_handle ), $child_version .'-'. $style_cache_buster ); | |
// will grab the script file from the child theme directory, and is reliant on jquery and the divi-custom-script (so it comes after that one) | |
wp_enqueue_script( 'fs-child-script', get_stylesheet_directory_uri() . '/script.js', array('jquery', 'divi-custom-script'), $child_version .'-'. $script_cache_buster, true); | |
} | |
add_action( 'wp_enqueue_scripts', 'fs_theme_enqueue_stuff' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment