We have been working on WordPress website optimization and want to replace or combine style sheets and do some other fancy stuff. In order to do this we need handle names of scripts and styles, we have two functions that works to retrieve the handle of a script or style. We used the WordPress Codex where you can find good and reliable information.

We need to retrieve the handles to dequeue and / or enqueue scripts or styles. For example if you need to combine scripts, you will need to dequeue the original handle in order to enqueue a new handle for your javascripts or CSS styles Check out those sources for more information in the Codex about dequeue or deregister scripts or styles and here to enqueue or register scripts or styles.

In another article we will explain how to combine scripts and styles for WordPress Page Speed Optimization.

Get a list of all handles of enqueued scripts

Here is a code snippet you can use to find the handles of scripts add it to the functions.php of your theme don’t worry only admins can see the notifications.

    # Finding handle for scripts - only admins can see this - uncomment when finished or delete
    function cloudzola_display_scripts_handles() {
        global $wp_scripts;
        if(current_user_can('manage_options') && ! is_admin()){
            foreach( $wp_scripts->queue as $handle ) :
            $obj = $wp_styles->registered [$handle];
            echo $filename = $obj->src;
            echo ' : <b>Handle for this Script is:</b> <span style="color:red"> '.$handle.'</span><br/><br/>';
            endforeach;
        }
    }
    add_action( 'wp_print_scripts', 'cloudzola_display_scripts_handles' );
    # END Finding handle for scripts - only admins can see this - uncomment when finished or delete

Get a list of all handles of enqueued styles

Here is a code snippet you can use to find the handles of styles, add it to the functions.php of your theme don’t worry only admins can see the notifications.

    # Finding handle for scripts - only admins can see this - uncomment when finished or delete
    function cloudzola_display_css_handles() {
        global $wp_styles;
        if(current_user_can('manage_options') && ! is_admin()){
            foreach( $wp_styles->queue as $handle ) :
            $obj = $wp_styles->registered [$handle];
            echo $filename = $obj->src;
            echo ' : <b>Handle for this Style is:</b> <span style="color:red"> '.$handle.'</span><br/><br/>';
            endforeach;
        }
    }
    add_action( 'wp_print_styles', 'cloudzola_display_css_handles' );
    # END Finding handle for scripts - only admins can see this - uncomment when finished or delete

For what do we need handles?

We need them to retrieve the handle name they are enqueued with, so we can use them to do things like deregister, dequeue and vice versa. For example we need them to conditionally load scripts and styles or to do research to find what we are looking for.

Use at own risk.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.