最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - jQuery dependent script ignores $in_footer bool and doesn't work in header

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论