3

I need a stylesheet to be added to the header before anything else. That means before any styles or scripts that are automatically added by any of the plugins I'm also using.

I figured I could add this stylesheet with wp_enqueue_script, but I'm not sure how to force it to be loaded before other stylesheets or scripts that I do not have control over.

Thanks.

*This is for a theme which I need to add a stylesheet to. This is not for a plugin I'm building.

2

2 Answers 2

2

The tag that you want to call to execute your function is the wp_head() with a priority of 1.

In your child theme's functions.php file, add the following:

add_action( 'wp_head', 'wpse_239006_style', 1 );
function wpse_239006_style() {
    wp_enqueue_script();
}
3
  • Is there a reason that wp_head is used here instead of wp_enqueue_scripts? Also, it's worth noting that negative numbers can be used for priorities too, the maximum being PHP_INT_MIN. This is probably not a great idea, just sayin'. Also, the toolbar (admin bar) and dashicons are given special treatment by WP, so if the user is logged in and the admin bar is configured to show, those styles will still appear before anything else regardless of the priority. Sep 12, 2016 at 21:48
  • @ethanjinksosullivan Thanks. My stylesheet is now loading as second in the header. For some reason the nivo slider stylesheet is still loading before it. I even set the priority to 0 instead of 1 because at 1 it was loading further down.
    – Mark.C
    Sep 12, 2016 at 22:38
  • @Mark.C Glad that worked. be sure to click the green checkmark next to my answer to indicate that your question has been solved Sep 12, 2016 at 22:48
0

To add stylesheet before other stylesheets, use wp_register_style() instead of wp_enqueue_style(), and then manually add your style to be first at queue:

wp_register_style('handle', get_template_directory_uri()."/custom.css");
array_unshift(wp_styles()->queue, 'handle');

Your Answer

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

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