Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit 8a170ca

Browse files
authored
Merge pull request #5 from creode/feature/stylesheet-dependencies
Added functionality to provide stylesheets that the main theme styles…
2 parents ba5e0bd + ff260a5 commit 8a170ca

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

includes/class-asset-enqueue.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ final class Asset_Enqueue {
2121
*/
2222
private static $instance = null;
2323

24+
/**
25+
* Array of stylesheet handles to be used as dependancies for enqueued stylesheets.
26+
*
27+
* @var string[]
28+
*/
29+
private $stylesheet_dependencies = array();
30+
2431
/**
2532
* Initializes the process.
2633
*/
@@ -149,8 +156,39 @@ private function enqueue_stylesheets( string $entrypoint ) {
149156
}
150157

151158
$asset = $manifest->getManifest()[ $entrypoint ];
152-
wp_enqueue_style( $asset['name'], $style['url'], array(), $style['hash'] );
159+
wp_enqueue_style( $asset['name'], $style['url'], $this->stylesheet_dependencies, $style['hash'] );
160+
}
161+
}
162+
163+
/**
164+
* Add a dependancy to be used for main stylesheets.
165+
*
166+
* @param string $handle A stylesheet handle.
167+
* @param string $src A stylesheet source.
168+
* @param array $dependencies (Optional) an optional array of stylesheet handles.
169+
* @param string $version (Optional) String specifying stylesheet version number.
170+
* @param string $media (Optional) The media for which this stylesheet has been defined. Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
171+
*/
172+
public function add_stylesheet_dependency( string $handle, string $src, array $dependencies = array(), string $version = '1', string $media = 'all' ) {
173+
if ( in_array( $handle, $this->stylesheet_dependencies, true ) ) {
174+
return;
153175
}
176+
177+
add_action(
178+
'wp_enqueue_scripts',
179+
function () use ( $handle, $src, $dependencies, $version, $media ) {
180+
wp_register_style(
181+
$handle,
182+
$src,
183+
$dependencies,
184+
$version,
185+
$media
186+
);
187+
},
188+
5
189+
);
190+
191+
array_push( $this->stylesheet_dependencies, $handle );
154192
}
155193

156194
/**

theme-template/functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@
2828

2929
// Register any scripts needed.
3030
require_once __DIR__ . '/includes/register-scripts.php';
31+
32+
// Register any stylesheet depandancies.
33+
require_once __DIR__ . '/includes/register-stylesheet-dependencies.php';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* File for registering stylesheet dependencies. For example third-party font provider integrations.
4+
*
5+
* @package :THEME_LABEL
6+
* @creode-wordpress-theme-version :THEME_PLUGIN_VERSION
7+
*/
8+
9+
use Creode_Theme\Asset_Enqueue;
10+
11+
// Asset_Enqueue::get_instance()->add_stylesheet_dependency(
12+
// 'typekit',
13+
// 'https://use.typekit.net/example.css'
14+
// );

0 commit comments

Comments
 (0)