Check if a script/style was enqueued/registered

Is it possible to test whether a script or a style was registered using wp_register_script/_style or wp_enqueue_script/_style? All functions doesn’t return a value and I’m completely clueless.

I need it to switch between different functions depending on stylesheet-libraries and scripts I offer.

Thank you!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There is a function called wp_script_is( $handle, $list ). $list can be one of:

  • ‘registered’ — was registered through wp_register_script()
  • ‘queue’ — was enqueued through wp_enqueue_script()
  • ‘done’ — has been printed
  • ‘to_do’ — will be printed

Ditto all that for wp_style_is().

Method 2

Check $GLOBALS['wp_scripts']->registered for scripts.

Example

function is_enqueued_script( $script )
{
    return isset( $GLOBALS['wp_scripts']->registered[ $script ] );
}

print (int) is_enqueued_script( 'l10n' );

$GLOBALS['wp_styles']->registered works the same way.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x