I am developing a plugin that uses Twitter Bootstrap Modals, but would like to prevent loading bootstrap if it's already loaded.

<a href="https://github.com/MaxCDN/bootstrap-cdn/issues/111#issuecomment-14467221">Apparently</a> 

The following .js code could do the trick:

if(typeof($.fn.modal) === 'undefined') {
 document.write('<script src="path_to_/bootstrap-3.1.1.js"><\/script>')

But is there any way of doing it with PHP, or otherwise preventing the plugin from enqueuing the script if it's functions are already present?

  • why do you think it's gonna load more than once?
    – Tomás Cot
    Sep 17 '14 at 2:18
  • that's a good question, man. when the plugin file bootstrap is present, there's a conflict with the main nav in customizr. and it seems to load nested modals.
    – MikeiLL
    Sep 17 '14 at 3:52
  • If it's included by more then one plugins then it could possible. Not possible to detect with php. stackoverflow.com/questions/13933000/…
    – Robert hue
    Sep 17 '14 at 3:53
  • @Roberthue yea i read that thread, which is where the link and code above came from.
    – MikeiLL
    Sep 17 '14 at 3:56

There is no good way of doing this.

One way is to enumerate the already registered scripts, like this:

    function yourplugin_add_my_stylesheet() {
      global $wp_styles;
      if ( $wp_styles instanceof WP_Styles ) {
        // enumerate all current styles
        foreach( $wp_styles->queue as $handle ) {  
          // use the $handle to see if it's called 'bootstrap'
          $obj = $wp_styles->registered[$handle];
          // or use $obj['src'] to see if `bootstrap.css` is in there
    add_action('wp_enqueue_scripts', 'yourplugin_add_my_stylesheet');

But this will in most cases not work:

  1. The order of plugin loading is not know, so the offending other plugin my load after this.
  2. The offending other plugin my not use terms like 'bootstrap', or may load the css via other means, not the wp_register_style() method.

A possible suggestion is that your plugin gets a option page with a checkbox that allows the admin to either load the css or not. This way, if there is a conflict or duplication then one can switch off the loading of the duplicate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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