Proper way to replace enqueued scripts in wordpress?

I need to remove jquery ui scripts (and some others) from wordpress the problem is if I dequeue and deregister them then plugins that call them don’t work because the scripts are “missing” even though I’m loading all of jquery ui with one script through the googleapis link.

I have the following:

Read More
add_action('wp_print_scripts','remove_excess_scripts', 100);
function remove_excess_scripts(){
  wp_dequeue_script( 'jquery-ui-core' );
  wp_dequeue_script( 'jquery-ui-widget' );
  wp_dequeue_script( 'jquery-ui-mouse' );
  wp_dequeue_script( 'jquery-ui-accordion' );
  wp_dequeue_script( 'jquery-ui-autocomplete' );
  wp_dequeue_script( 'jquery-ui-slider' );
  wp_dequeue_script( 'jquery-ui-progressbar' );
  wp_dequeue_script( 'jquery-ui-tabs' );
  wp_dequeue_script( 'jquery-ui-sortable' );
  wp_dequeue_script( 'jquery-ui-draggable' );
  wp_dequeue_script( 'jquery-ui-droppable' );
  wp_dequeue_script( 'jquery-ui-selectable' );
  wp_dequeue_script( 'jquery-ui-position' );
  wp_dequeue_script( 'jquery-ui-datepicker' );
  wp_dequeue_script( 'jquery-ui-tooltip' );
  wp_dequeue_script( 'jquery-ui-resizable' );
  wp_dequeue_script( 'jquery-ui-dialog' );
  wp_dequeue_script( 'jquery-ui-button' );

  wp_deregister_script( 'jquery-ui-core' );
  wp_deregister_script( 'jquery-ui-widget' );
  wp_deregister_script( 'jquery-ui-mouse' );
  wp_deregister_script( 'jquery-ui-accordion' );
  wp_deregister_script( 'jquery-ui-autocomplete' );
  wp_deregister_script( 'jquery-ui-slider' );
  wp_deregister_script( 'jquery-ui-progressbar' );
  wp_deregister_script( 'jquery-ui-tabs' );
  wp_deregister_script( 'jquery-ui-sortable' );
  wp_deregister_script( 'jquery-ui-draggable' );
  wp_deregister_script( 'jquery-ui-droppable' );
  wp_deregister_script( 'jquery-ui-selectable' );
  wp_deregister_script( 'jquery-ui-position' );
  wp_deregister_script( 'jquery-ui-datepicker' );
  wp_deregister_script( 'jquery-ui-tooltip' );
  wp_deregister_script( 'jquery-ui-resizable' );
  wp_deregister_script( 'jquery-ui-dialog' );
  wp_deregister_script( 'jquery-ui-button' );
}

Plus earlier in the functions I properly register and enqueue the jquery ui with:

wp_register_script(
        'jquery-ui-all',
        "http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
        array('jquery'),
        false,
        false
    );

and

function print_scripts() {
wp_enqueue_script( 'the-bootstrap' );   
}
add_action( 'wp_enqueue_scripts', 'print_scripts' );

(where the-bootstrap has dependencies for jquery-ui-all)

All of this works in the sense that jquery-ui is loaded from google and none of the individual UI files are output anymore – the problem comes because a plugin WPFullCalendar enqueues its script with a bunch of the jquery ui individual scripts as dependencies and I guess with them deregistered and dequeued its script doesn’t get output since the dependencies are missing? I get the following error (due to its JS file not being output)

Uncaught ReferenceError: WPFC is not defined 

How can I replace scripts so other plugins can still enqueue them or depend on them but they all output the same file. I can do this be deregistering all the names and then registering them again with googleapis as the source but then I’ll still get 15 different HTTP requests for all the same googleapi link… I need all of these files to point to the 1 googleapi file and only for one to output. Is this possible? (and shouldn’t dequeue take them out of the queue after the other plugins already called/depended on them successfully – wp_print_scripts happens after all the plugin enqueues, no?)

UPDATE:

I’ve modified the accepted answer below to be a little more specific so as not to break anything else as far as dependencies are concerned. Here is the update code to properly deregister a script and remove it’s dependency from any scripts that rely on. Be sure that if you do this, the script you use to replace the deregistered ones is loaded high enough in the HTML (probably the head) so that other scripts that depend on it come afterwards.

//custom deregister scripts
function georgian_deregister_scripts(){
    global $wp_scripts;
    //scripts to deregister
    $deregisteredscripts = array('jquery-ui-core','jquery-ui-widget','jquery-ui-mouse','jquery-ui-accordion','jquery-ui-autocomplete','jquery-ui-slider','jquery-ui-progressbar' ,'jquery-ui-tabs','jquery-ui-sortable','jquery-ui-draggable','jquery-ui-droppable','jquery-ui-selectable','jquery-ui-position','jquery-ui-datepicker','jquery-ui-tooltip','jquery-ui-resizable','jquery-ui-dialog','jquery-ui-button');
    //degregister each script
    foreach($deregisteredscripts as $script)
      wp_deregister_script( $script );
    //remove deregistered scripts as dependencies of any other scripts depending on them  
    if(false != $wp_scripts->queue)
      foreach($wp_scripts->queue as $script)
        if(isset($wp_scripts->registered[$script]))
          $wp_scripts->registered[$script]->deps = array_diff($wp_scripts->registered[$script]->deps, $deregisteredscripts);
}
add_action('wp_enqueue_scripts', 'georgian_deregister_scripts', 101);

Related posts

Leave a Reply

1 comment

  1. If you deregister script, it does not get enqueued, so you have a couple of useless commands.

    Now, what about removing dependencies on (all) scripts to be enqueued? This function should do the trick.

    function customize_scripts_output(){
        global $wp_scripts;
    
        if(false != $wp_scripts->queue)
            foreach($wp_scripts->queue as $script)
                if(isset($wp_scripts->registered[$script]))
                    $wp_scripts->registered[$script]->deps = array();
    }
    add_action('wp_enqueue_scripts', 'customize_scripts_output', 101);