Get only enqueued styles and scripts of the current theme

The first argument of preg_match is supposed to be a pattern, not a string, so it’s probably not comparing the way you expect. Use strpos instead:

function wpse_275760_theme_scripts() {
    global $wp_scripts;

    $stylesheet_uri = get_stylesheet_directory_uri();

    foreach( $wp_scripts->queue as $handle ) {
        $obj = $wp_scripts->registered[$handle];
        $obj_handle = $obj->handle;
        $obj_uri = $obj->src;

        if ( strpos( $obj_uri, $stylesheet_uri ) === 0 )  {
            echo $obj_handle;
        } else {
            echo 'NOTHING Match';
        }
    }
}

strpos() returns the starting position of the match, if there is one. Comparing with === to 0 ensures that the script URL and theme URL match from the beginning, which is what you want, since they will both start with http://mydomain/wp-content/themes/mytheme.

Leave a Comment