Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I enqueue external scripts using wp_enqueue_script()?

Tags:

wordpress

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 );
like image 734
Tomasz Muter Avatar asked Feb 05 '16 12:02

Tomasz Muter


1 Answers

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.

like image 79
rnevius Avatar answered Oct 12 '22 22:10

rnevius