Check the Version of an Enqueue’d External Library

I want to use an external font library in my plugin, but I want to do a version check first to determine if I should bother enqueue’ing my version or not. I know I can see if a library is already loaded — wp_script_is() — but I don’t know how to check the library’s version. Any ideas?

    if ( wp_script_is() || wp_script_is( 'registered' ) ) {
        \\ version check here
    }

1 Answer
1

I can’t find a nice way of doing it though wp_script_*, but you can go to the global directly:

global $wp_scripts;
$version = $wp_scripts->query( 'jquery' )->ver;

Here the query returns the script’s _WP_Dependency object, and then we can get the version from it. (Annoyingly wp_script_is( ..., 'registered' ) fetches the _WP_Dependency but then casts the result to a bool to return.)

This only works for registered scripts; apologies I don’t know the distinction, but AFAICS a script can only be queued if it’s registered, and it’s only the registered data that holds the version.

Leave a Comment