Skip to content

Instantly share code, notes, and snippets.

@sergeliatko
Last active February 7, 2019 11:35
Show Gist options
  • Save sergeliatko/6c4a7d1c9f20776322612f3d47a29bef to your computer and use it in GitHub Desktop.
Save sergeliatko/6c4a7d1c9f20776322612f3d47a29bef to your computer and use it in GitHub Desktop.
Returns current stylesheet registration handle.
<?php
/**
* Returns current stylesheet handle used in wp_register_style() function or NULL if not found.
*
* @return string|null
*/
function get_current_stylesheet_handle() {
$pattern = sprintf(
'/%1$s\/style\.(min\.)?css/',
str_replace( '/', '\/', untrailingslashit( get_stylesheet_directory_uri() ) )
);
/** @var \_WP_Dependency $style */
foreach ( (array) wp_styles()->registered as $style ) {
if ( preg_match( $pattern, $style->src ) ) {
return $style->handle;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment