我应该使用 wp_enqueue_script() 将外部脚本排入队列吗? [英] Should I enqueue external scripts using wp_enqueue_script()?

查看:25
本文介绍了我应该使用 wp_enqueue_script() 将外部脚本排入队列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用该函数将外部脚本排入队列是否正确?它有效,但不会减慢打开网站的速度吗?

It is correct to enqueue external scripts with that function? It works but doesn't it slow down opening the site?

wp_register_script( 'google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=true', null, null, true );
wp_register_script( 'jsapi', 'https://www.google.com/jsapi', null, null, true );
wp_register_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', null, null, true );
wp_register_script( 'unveil', get_template_directory_uri() . '/new-js/jquery.unveil.min.js', null, null, true );

推荐答案

不,它不会减慢网站的速度(至少不足以成为不使用它的理由).这是在 WordPress 中排队任何类型的脚本的正确方法.此函数所做的只是将适当的 标签(带有依赖项)添加到您的 HTML .

No, it doesn't slow the site (at least not enough to make it a reason for not using it). This is the correct way to enqueue any type of script in WordPress. All this function does is add the proper <link> tags (with dependencies) to your HTML <head>.

但是,出于某种原因,您没有在代码中包含依赖项.例如,Bootstrap 需要 jQuery,因此您应该将其包含在您的函数中:

However, for some reason, you haven't included dependencies in your code. For example, Bootstrap requires jQuery, so you should include it in your function:

wp_enqueue_script( 'bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true );

如上所示,您还应该考虑使用 wp_enqueue_script() 而不是 wp_register_script(),因为它可以为您节省一步.

As shown above, you should also consider using wp_enqueue_script() instead of wp_register_script(), since it will save you a step.

这篇关于我应该使用 wp_enqueue_script() 将外部脚本排入队列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆