I'm searching for the "right way" to add a custom.css file after the 3rd party plugin has loaded theirs. I know how to register and enqueue my css files in functions.php. Can I somehow add a dependency for the 3rd party plugin stylesheets? For example I want to load a custom.css file afer the plugin woocomerce has loaded. I want this without editing the plugin. Can this be done in functions.php?
1 Answer
The most correct way to do this would be to just utilize woocommerce's general stylesheet as a dependency using wp_enqueue_style().
add_action( 'wp_enqueue_scripts', 'my_woocommerce_styles' );
function my_woocommerce_styles(){
wp_enqueue_style( 'my-wc-style', 'path/to/my-styles.css', array( 'woocommerce-general' ), '1.0' );
}
Woocommerce has multiple stylesheets registered under different handles - but you're most likely looking for woocommerce-general.
Using it as a dependency like that will only load it if woocommerce-general has been loaded for that particular request, and it will load it afterwards. You may need to adjust the priority of the add_action depending on exactly how/where you're doing this. You mentioned functions.php so it shouldn't be necessary, but you if you need to, just pass another priority to it, for example:
add_action( 'wp_enqueue_scripts', 'my_woocommerce_styles', 15 );