To load a script in wordpress, you have to use the function wp_enqueue_script
Often people call the function like this
wp_enqueue_script('my-script', get_template_directory_uri() .'/js/my-script.js');
However, you may come across dependency problems if you do it like the above. Hence, it is best practice to use the other parameters, especially the third one which specifies the dependency. For example, if your script relies on jquery,
wp_enqueue_script('my-script', get_template_directory_uri() .'/js/my-script.js', array('jquery'));
Also, you can check if jquery is already enqueued by using
if ( ! wp_script_is( 'jquery', 'enqueued' ) ) { wp_enqueue_script( 'jquery' ); }
Finally, note that wp_enqueue_script
is for the front-end of your site. If you wish to queue scripts for the admin panel, useĀ admin_enqueue_scripts
Be First to Comment