9

First, I'm aware that my question is happening in the context of my work with the WooCommerce plugin, which would normally make it off-topic. However, I think my question relates to wp_enqueue_script, so hopefully it is still on topic.

So WooCommerce is registering a script on the admin_enqueue_scripts hook. This script requires a bunch of dependencies:

wp_register_script( 'wc-admin-meta-boxes', WC()->plugin_url() . '/assets/js/admin/meta-boxes' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'accounting', 'round', 'ajax-chosen', 'chosen', 'plupload-all' ), WC_VERSION );

(it is enqueued specifically on the post.php and post-new.php page for the product post type a little later on in the code)

In the custom plugin that I am writing to work with WooCommerce I am also loading a script on the same hook.

wp_enqueue_script( 'My_Plugin_Metabox', My_Plugin_Class()->plugin_url() . '/assets/js/mnm-write-panel.js', array( 'jquery', 'wc-admin-meta-boxes'), My_Plugin_Class()->version, true );

If I enqueue my plugin's script and set the $in_footer parameter to true then inexplicably, the jQuery UI Datepicker script is not loaded (not in the source code at all) and the console shows corresponding script errors.

If I load my script in the header, this is not a problem. If I load my script without the wc-admin-meta-boxes dependency, then that also resolves the problem

So what I am wondering is, why does loading my script in the footer effect the loading of the core datepicker script? (I'm not using datepicker in my script at all.) Or why not having the Woo script as a dependency would also effect the datepicker script? It seems to me that the datepicker script should be loaded no matter what as a dependency of the Woo metabox script, but this isn't happening.

Per Kaiser's comment, I created the following MU plugin (adjusted from comments because $GLOBALS['wp_scripts'] is an object:

/* Plugin Name: Dump jQUI Dp */ 

add_action( 'shutdown', 'so_dump_query_ui_dependencies' );
function so_dump_query_ui_dependencies() {  
    echo 'Does jQuery UI DatePicker script exist per default in&hellip;?<br>';  
    $s = 'jquery-ui-datepicker';    
    printf( 'The registered Dependencies Array: %s', isset( $GLOBALS['wp_scripts']->registered[ $s ] ) ? 'yep ' : 'nope ' );    
    printf( 'The Dependencies loaded in the footer: %s', isset( $GLOBALS['wp_scripts']->in_footer[ $s ] ) ? 'yep ' : 'nope ' );     
    printf( 'The Dependencies printed to the DOM: %s', isset( $GLOBALS['wp_scripts']->done[ $s ] ) ? 'yep ' : 'nope ' );    
    echo 'All nope? Well, then&hellip;'; 
}

With only WooCommerce 2.2.8 active, the result reads:

The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope

With WooCommerce 2.2.8 plus my new "dummy" plugin the result reads the same (whether my script is loaded in the footer or not):

The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope

Dummy Plugin

Also per the comments, here's a dummy plugin to hopefully reproduce the issue for others. I stripped my existing plugin all the way down to only load a script on the product post type admin pages. I am still seeing datepicker load when $in_footer is false and not load when $in_footer is true.

<?php
/*
Plugin Name: WooCommerce Dummy Plugin
Plugin URI: http://wordpress.stackexchange.com/q/168688/6477
Author: helgatheviking
Description: Enqueue a script, miraculously dequeue datepicker
*/


/**
 * The Main My_Dummy_Plugin class
 **/
if ( ! class_exists( 'My_Dummy_Plugin' ) ) :

class My_Dummy_Plugin {

    /**
     * @var My_Dummy_Plugin - the single instance of the class
     */
    protected static $_instance = null;

    /**
     * variables
     */
    public $version = '1.0.0';

    /**
     * Main My_Dummy_Plugin instance.
     *
     * Ensures only one instance of My_Dummy_Plugin is loaded or can be loaded
     *
     * @static
     * @return My_Dummy_Plugin - Main instance
     */
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    /**
     * Cloning is forbidden.
     */
    public function __clone() {
        _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ) );
    }


    /**
     * Unserializing instances of this class is forbidden.
     */
    public function __wakeup() {
        _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ) );
    }


    /**
     * My_Dummy_Plugin Constructor
     *
     * @access  public
     * @return  My_Dummy_Plugin
     */
    public function __construct() {

        add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );

    }


    /*-----------------------------------------------------------------------------------*/
    /* Helper Functions */
    /*-----------------------------------------------------------------------------------*/

    /**
     * Get the plugin url.
     *
     * @return string
     */
    public function plugin_url() {
        return untrailingslashit( plugins_url( '/', __FILE__ ) );
    }


    /**
     * Get the plugin path.
     *
     * @return string
     */
    public function plugin_path() {
        return untrailingslashit( plugin_dir_path( __FILE__ ) );
    }

    /*-----------------------------------------------------------------------------------*/
    /* Load scripts */
    /*-----------------------------------------------------------------------------------*/

    public function admin_scripts() {

        // Get admin screen id
        $screen = get_current_screen();

        // Product post type page only
        if ( in_array( $screen->id, array( 'product' ) ) ) {

            wp_enqueue_script( 'My_Dummy_Plugin_Metabox', $this->plugin_url() . '/assets/js/metabox.js', array( 'jquery', 'wc-admin-meta-boxes'), $this->version, true );

        }

    }

} //end class: do not remove or there will be no more guacamole for you

endif; // end class_exists check


/**
 * Returns the main instance of My_Dummy_Plugin
 *
 * @return WooCommerce
 */
function My_Dummy_Plugin() {
    return My_Dummy_Plugin::instance();
}

// Launch the whole plugin
My_Dummy_Plugin();
  • 1
    Just curious. Have you attempted to set the priority of your action queueing your scripts either above or below that of WooCommerce when queueing in the footer? I've run into instances with plugins using identical dependencies and it deregistering every instance of it, and for some reason this fixed it. (I didn't give it much thought). Never made a difference between queueing in the header vs. footer though. – BODA82 Nov 18 '14 at 2:19
  • I wonder what happened to all the other comments? Anyway, @BODA82, no I had not tried that. But adding a priority does keep datepicker loading correctly even when $in_footer is true on my own script. – helgatheviking Nov 18 '14 at 8:39
  • 1
    This looks like a bug in WP to me - I haven't followed through the logic but if you look at the function do_items in "wp-includes/class.wp-dependencies.php", at lines 122-125, the code just unsets the item in the to_do list whether or not do_item succeeds. If you change those lines to if ( $this->do_item( $handle, $group ) ) { $this->done[] = $handle; unset( $this->to_do[$key] ); } then the bug goes away... – bonger Nov 18 '14 at 17:16
  • 2
    This is a WP bug - see trac #25247. I proposed a patch (gitlost c'est moi). – bonger Dec 13 '14 at 6:36
  • @bonger Thanks for the definitive answer. If you wanted to move your comment to an answer I'd accept it. Apparently, dependencies are hell. – helgatheviking Dec 13 '14 at 13:43
2

Currently you can force a loading for the Libraries using wp_enqueue_script(), like so:

wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui');

I know it shoud load automatically, but it works that way.

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.