Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion includes/class-asset-enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ final class Asset_Enqueue {
*/
private static $instance = null;

/**
* Array of stylesheet handles to be used as dependancies for enqueued stylesheets.
*
* @var string[]
*/
private $stylesheet_dependencies = array();

/**
* Initializes the process.
*/
Expand Down Expand Up @@ -149,8 +156,39 @@ private function enqueue_stylesheets( string $entrypoint ) {
}

$asset = $manifest->getManifest()[ $entrypoint ];
wp_enqueue_style( $asset['name'], $style['url'], array(), $style['hash'] );
wp_enqueue_style( $asset['name'], $style['url'], $this->stylesheet_dependencies, $style['hash'] );
}
}

/**
* Add a dependancy to be used for main stylesheets.
*
* @param string $handle A stylesheet handle.
* @param string $src A stylesheet source.
* @param array $dependencies (Optional) an optional array of stylesheet handles.
* @param string $version (Optional) String specifying stylesheet version number.
* @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)'.
*/
public function add_stylesheet_dependency( string $handle, string $src, array $dependencies = array(), string $version = '1', string $media = 'all' ) {
if ( in_array( $handle, $this->stylesheet_dependencies, true ) ) {
return;
}

add_action(
'wp_enqueue_scripts',
function () use ( $handle, $src, $dependencies, $version, $media ) {
wp_register_style(
$handle,
$src,
$dependencies,
$version,
$media
);
},
5
);

array_push( $this->stylesheet_dependencies, $handle );
}

/**
Expand Down
3 changes: 3 additions & 0 deletions theme-template/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@

// Register any scripts needed.
require_once __DIR__ . '/includes/register-scripts.php';

// Register any stylesheet depandancies.
require_once __DIR__ . '/includes/register-stylesheet-dependencies.php';
14 changes: 14 additions & 0 deletions theme-template/includes/register-stylesheet-dependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* File for registering stylesheet dependencies. For example third-party font provider integrations.
*
* @package :THEME_LABEL
* @creode-wordpress-theme-version :THEME_PLUGIN_VERSION
*/

use Creode_Theme\Asset_Enqueue;

// Asset_Enqueue::get_instance()->add_stylesheet_dependency(
// 'typekit',
// 'https://use.typekit.net/example.css'
// );