Register Stylesheet if a particular plugin is installed

Viewed 28

I use a basic custom plugin I have built for various WordPress admin functions.

One of them adds a custom CSS file that hooks into the admin as per the example below:

// Sets A Custom Admin Colour Scheme
function hits_admin_colour_scheme() {
    wp_register_style('hits_admin_colour_scheme', plugins_url('/assets/css/admin.css',__FILE__ ));
    wp_enqueue_style('hits_admin_colour_scheme');
}
add_action( 'admin_init','hits_admin_colour_scheme');

Is it possible to have an additional stylesheet that is only loaded if a particular plugin is installed? Say WooCommerce or ACF? This way I can reduce the size of the default CSS file and only load what is relevant.

Sorry if this is a newbie question. I appreciate any help.

1 Answers

You can check whether woocommerce(or others by same way) is activated before enqueue the file

function hits_admin_colour_scheme() {
    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
        wp_register_style('hits_aditional_colour_scheme', plugins_url('/assets/css/additional.css',__FILE__ ));
        wp_enqueue_style('hits_aditional_colour_scheme');
    }
}
add_action( 'admin_enqueue_scripts', 'hits_admin_colour_scheme');

and please use admin_enqueue_scripts hook to enqueue admin assets.

Related