Should I enquee external scripts using wp_enqueue_

2019-07-11 06:29发布

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
1条回答
老娘就宠你
2楼-- · 2019-07-11 06:45

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>.

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 );

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

查看更多
登录 后发表回答