JavaScript: TypeError: xyz is not a function after upgrading WordPress

After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.j

After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.js).

All scripts are added via wp_register_script and wp_add_inline_script WordPress' functions.

At bottom of <body> there are three scripts as always:

<script type='text/javascript' src='.min.js'></script>
<script type='text/javascript' src='.js'></script>
<script type='text/javascript'>
    var calendar = create_calendar();
</script>

Relevant part of calendar.js

function create_calendar()
{
    var calendar = $('#Calendar').clndr({
        // clndr options
    });

    return calendar;
}

All scripts should run in right order, but they are not working and in browser console I have this error: TypeError: $(...).clndr is not a function

Also, changing call to create_calendar in this way:

window.addEventListener("load", function(){
    var calendar = create_calendar();
});

changes only order of errors in console.

If I understand everything correctly, this should just work.. Any ideas? Or hints how to debug this?

After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.js).

All scripts are added via wp_register_script and wp_add_inline_script WordPress' functions.

At bottom of <body> there are three scripts as always:

<script type='text/javascript' src='http://example/clndr.min.js'></script>
<script type='text/javascript' src='http://example/calendar.js'></script>
<script type='text/javascript'>
    var calendar = create_calendar();
</script>

Relevant part of calendar.js

function create_calendar()
{
    var calendar = $('#Calendar').clndr({
        // clndr options
    });

    return calendar;
}

All scripts should run in right order, but they are not working and in browser console I have this error: TypeError: $(...).clndr is not a function

Also, changing call to create_calendar in this way:

window.addEventListener("load", function(){
    var calendar = create_calendar();
});

changes only order of errors in console.

If I understand everything correctly, this should just work.. Any ideas? Or hints how to debug this?

Share Improve this question edited Feb 16, 2020 at 16:52 acinis asked Feb 16, 2020 at 15:51 acinisacinis 12 bronze badges 2
  • jQuery isn't available as $ in WordPress. See wordpress.stackexchange/questions/2895/… – fuxia Commented Feb 16, 2020 at 15:55
  • Nice hint. I have custom jQuery included from beginning. Removing WordPress' one as in stackoverflow/a/27048128 fix all problems... They must change something, maybe naming of jQuery script handle.. – acinis Commented Feb 16, 2020 at 16:08
Add a comment  | 

1 Answer 1

Reset to default -1

According to @fuxia comment:

Solution is removing jQuery included by WordPress just before register/enqueue custom jQuery (see https://stackoverflow/a/27048128).

So if you include some jQuery on your own, then add

wp_dequeue_script('jquery');
wp_deregister_script('jquery');

just before register/enqueue custom jQuery script in wp_enqueue_scripts action.

Or if you want jQuery included by WordPress just remember, that it's in no-conflict mode and $ is undefined. See https://wordpress.stackexchange/a/2896/182809 for more details.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744744331a4591216.html

相关推荐

  • JavaScript: TypeError: xyz is not a function after upgrading WordPress

    After upgrading WordPress from 4.9.13 to 5.3.2 some scripts on website stop working in weird way... (example for clndr.j

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信