1

I am trying to delay the loading of a css file which is blocking the other resources from loading first during the loading of the website page.

I am supposed to add this code to the functions.php file:

function delay_css_loading() {
    wp_enqueue_style( 'your-css-handle', get_template_directory_uri() . '/path/to/your.css', array(), '1.0', 'all' );
}
add_action( 'wp_enqueue_scripts', 'delay_css_loading', 999 );

For this I have the url of the CSS file but not the handle ('your-css-handle') for the same.

How do I find the Handle of the css file in wordpress?

Tried extracting the id from the url as a the handle, but ir didnt work.

1 Answers1

0

This webpage provides instructions on how to get the handles of the stylesheets running on a WordPress instance.

We can modify it slightly to output both the handle and the source (where available). This shall be the code you need.

function print_stylesheets() {
    // Print all loaded Styles (CSS) and their URL
    global $wp_styles;
    foreach( $wp_styles->queue as $handle ) :
        $style = $wp_styles->registered[$handle];
        if ( $style->src ) {
            echo '<p><strong>' . $handle . '</strong> - ' . $style->src . '</p>';
        } else {
            echo '<p><strong>' . $handle . '</strong></p>';
        }
    endforeach;
}
add_action( 'wp_print_scripts', 'print_stylesheets' );
  • I got this sample output from using the mentioned code: litespeed-cache - https://www.shahzadpurfarmyoga.com/wp-content/plugins/litespeed-cache/assets/css/litespeed.css The handle is the last part (litespeed.css) right? – Randeep Singh Aug 13 '23 at 08:57
  • 1
    The handle for litespeed.css seems to be set to Core::PLUGIN_NAME (see /src/gui.cls.php:366) which is defined in /src/core.cls.php:18 to 'litespeed-cache'. The handle is thus 'litespeed-cache'. – Jamie Blomerus Aug 13 '23 at 09:40
  • Yes, it worked fo me. – Randeep Singh Aug 13 '23 at 14:58
  • Excellent, if you find my answer helpful I would appreciate it if you would mark it as the accepted answer as it helps future people with the same problem to find a solution. – Jamie Blomerus Aug 13 '23 at 18:11