I am trying to get ONLY the loaded styles and scripts of the current active theme. I am using preg_match to single out just the theme's script filenames. If matches are found, then just echo the handles of those filenames so I can dequeue/deregister them.

But the preg_match is not working. I am trying to partial match the current theme url ($currthemeurl) with the filename url ($filenames) of the script. Please notice my commented comments.

function remove_theme_scripts() {

    global $wp_scripts;

    $currthemeurl = get_stylesheet_directory_uri(); 
    //Shows http://mydomain/wp-content/themes/mytheme

    foreach( $wp_scripts->queue as $handle ){

             $obj = $wp_scripts->registered [$handle];
             $handles = $obj->handle;  //something.js
             $filenames = $obj->src; //SHOWS http://mydomain/wp-content/themes/mytheme/js/something.js

            if (preg_match($currthemeurl, $filenames)):
             //MATCH FOUND

             echo $handles;
             echo "NOTHING Match";


1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.