Is This The Most Efficient Way To Add Javascript Files?

This is the proper way to load scripts. Whether it’s effecient will depend on when you actually need to load the scripts. Currently it will load on every front end page. If this is not necessary, you should put some conditionals and call wp_enqueue_scripts when the file is actually needed.

Also if you are loading a script you don’t have to manually queue its dependencies (other scripts it requires to have been loaded to work). For instance wp_enqueue_script('custom'); will automatically load ‘gmaps_google’ and ‘gmap’ as well – so you don’t need to tell WordPress load them.

If you are simply registering, then immediately enqueuing the scripts you can neaten up the code by providing the script’s location (and dependencies etc) in wp_enqueue_scripts and not registering them. However, usually it’s best to register the scripts and then enqueue them only when needed (e.g. in a shortcode or widget callback) depending on the context.

Finally, wp_enqueue_scripts only fires on the front-end so the !is_admin check is superfluous.

Leave a Comment