admin管理员组

文章数量:1022553

My plugin uses the following to enqueue script in jQuery dependency:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

If I use it on child theme where I dequeue default WordPress jQuery and load jQuery from CDN in footer, both jQuery and my script land in footer and it works correctly.

Unfortunately when I use plugin on default theme, where default WordPress jQuery is loaded in header, $in_footer is ignored, script appears in header after jQuery and doesn't work despite no errors in console.

I wouldn't like to replace default jQuery or even move it to the footer, because it can break other things for users.

My script is the following, phpVars is properly passed in localized script, above in generated HTML:

var timeout;

jQuery('div.woocommerce').on('change', '.qty', function(){
    if (timeout != undefined) clearTimeout(timeout);        
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, phpVars.acau_update_delay );
});

EDIT:

Apparently jQuery dependence makes no difference, if I specify an empty array instead, plugin script is still loaded in header below jQuery and doesn't work, this time only with version parameter instead of both version and jQuery ?ver%5B0%5D=jquery.

My plugin uses the following to enqueue script in jQuery dependency:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

If I use it on child theme where I dequeue default WordPress jQuery and load jQuery from CDN in footer, both jQuery and my script land in footer and it works correctly.

Unfortunately when I use plugin on default theme, where default WordPress jQuery is loaded in header, $in_footer is ignored, script appears in header after jQuery and doesn't work despite no errors in console.

I wouldn't like to replace default jQuery or even move it to the footer, because it can break other things for users.

My script is the following, phpVars is properly passed in localized script, above in generated HTML:

var timeout;

jQuery('div.woocommerce').on('change', '.qty', function(){
    if (timeout != undefined) clearTimeout(timeout);        
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, phpVars.acau_update_delay );
});

EDIT:

Apparently jQuery dependence makes no difference, if I specify an empty array instead, plugin script is still loaded in header below jQuery and doesn't work, this time only with version parameter instead of both version and jQuery ?ver%5B0%5D=jquery.

Share Improve this question edited May 3, 2019 at 20:23 Ryszard Jędraszyk asked May 3, 2019 at 20:02 Ryszard JędraszykRyszard Jędraszyk 3244 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It's all about poor typo, I put one extra parameter '' before dependency parameter:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

should be:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', array('jquery'), '', true);

Now my script properly loads in footer.

My plugin uses the following to enqueue script in jQuery dependency:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

If I use it on child theme where I dequeue default WordPress jQuery and load jQuery from CDN in footer, both jQuery and my script land in footer and it works correctly.

Unfortunately when I use plugin on default theme, where default WordPress jQuery is loaded in header, $in_footer is ignored, script appears in header after jQuery and doesn't work despite no errors in console.

I wouldn't like to replace default jQuery or even move it to the footer, because it can break other things for users.

My script is the following, phpVars is properly passed in localized script, above in generated HTML:

var timeout;

jQuery('div.woocommerce').on('change', '.qty', function(){
    if (timeout != undefined) clearTimeout(timeout);        
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, phpVars.acau_update_delay );
});

EDIT:

Apparently jQuery dependence makes no difference, if I specify an empty array instead, plugin script is still loaded in header below jQuery and doesn't work, this time only with version parameter instead of both version and jQuery ?ver%5B0%5D=jquery.

My plugin uses the following to enqueue script in jQuery dependency:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

If I use it on child theme where I dequeue default WordPress jQuery and load jQuery from CDN in footer, both jQuery and my script land in footer and it works correctly.

Unfortunately when I use plugin on default theme, where default WordPress jQuery is loaded in header, $in_footer is ignored, script appears in header after jQuery and doesn't work despite no errors in console.

I wouldn't like to replace default jQuery or even move it to the footer, because it can break other things for users.

My script is the following, phpVars is properly passed in localized script, above in generated HTML:

var timeout;

jQuery('div.woocommerce').on('change', '.qty', function(){
    if (timeout != undefined) clearTimeout(timeout);        
    timeout = setTimeout(function() {
        jQuery('[name="update_cart"]').trigger('click');
    }, phpVars.acau_update_delay );
});

EDIT:

Apparently jQuery dependence makes no difference, if I specify an empty array instead, plugin script is still loaded in header below jQuery and doesn't work, this time only with version parameter instead of both version and jQuery ?ver%5B0%5D=jquery.

Share Improve this question edited May 3, 2019 at 20:23 Ryszard Jędraszyk asked May 3, 2019 at 20:02 Ryszard JędraszykRyszard Jędraszyk 3244 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It's all about poor typo, I put one extra parameter '' before dependency parameter:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'), '', true);

should be:

wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', array('jquery'), '', true);

Now my script properly loads in footer.

本文标签: plugin developmentjQuery dependent script ignores infooter bool and doesn39t work in header