Conditional check to see if a bootstrap.css is included in a theme

I am creating a shortcode plugin for Bootstrap framework that will go along with my theme. However since this would be released to the plugin, if a users’s theme already has Bootstrap.css included, is there a way for me to check this from a plugin?

Related posts

2 comments

  1. You might be able to use wp_style_is() to do this:

    $style = 'bootstrap';
    if( ( ! wp_style_is( $style, 'queue' ) ) && ( ! wp_style_is( $style, 'done' ) ) ) {
        //queue up your bootstrap
        wp_enqueue_style( $style, $path_to_bootstrap, $deps, $ver, $media );
    }
    

    This comes with all the same caveats as @Milo’s solution — it’ll only work if the copy of bootstrap.css was registered or enqueued with a handle of bootstrap.

    References

  2. There’s no bulletproof way of doing this, as a theme developer could add the bootstrap.css file directly to a theme file, or enqueue it under any number of handles, or rename the css file to something else.

    You could check the $wp_styles global for bootstrap in a handle or filename, but this may fail (as noted above) or give you a false positive if bootstrap happens to be in a handle or file unrelated to your bootstrap. It’s also possible to build custom versions of bootstrap, so even if you did locate it, it may not contain all of the styles it’s assumed to contain.

    Here’s a quick example of peeking into the enqueued styles:

    function wpa_inspect_styles(){
        global $wp_styles;
        var_dump( $wp_styles );
    }
    add_action( 'wp_enqueue_scripts', 'wpa_inspect_styles', 9999 );
    

    Probably the best option, in my opinion, is to do some cursory checking and inform the user that you may have detected bootstrap in their theme, enqueue the file by default, and provide an option to disable the stylesheet.

Comments are closed.