How to Add Scripts to WordPress Sites?
Here is an example for you, now, you need to assume that this is for the .php file and theme is the parent theme.
Now, you need to break this down. The hook used is the wp_enqueue_scripts(), which makes sure that the function registers and enqueues script in the front end only.
Now start registering the scripts for the following:
Handle– this is what the script is named. You can enqueue your script this way and if you don’t want this, suppose you have a child theme, then the same can be dequeued.
Source- this is the file’s path. When you do coding for the path, do not use hard code. Use simple WordPress function. For child themes, there are different scripts, and you can find them easily for WordPress. One example is the get_template_directory_uri().
These are the scripts, your scripts rely upon. You can also pass multiple dependent scripts in the same array. In case there has not be an enqueue for a script easier, WordPress knows to do it at this moment of running the code.
This is the version of the script you load currently. If the script is updated, the version is bumped. When the version is changed in the script, WordPress changes it and loads the new one. Also, if there are any caches of the previous version, the same is reset now.
Load in Footer:
When the script is set as true for this, the script gets loaded in the footer. So, it takes to load after the entire page is loaded. This is better when you insert sliders. This is because, the slider scripts do not prevent important scripts getting loaded on the page.
The script is placed in the line of enqueue when the script is run. WordPress knows if the script has been on line already. This makes sure that the scripts are loaded only once and not repeated. When jQuery is loaded many times it is easy to know, what is the issue for not loading the script in the first attempt.
Wp_enqueue_script() and the wp_register_script() use the same arguments. You need to register the script first and then it is just enough to handle the scripts. Enqueuing scripts can be done in the specific pages alone. The script is loaded in the footer automatically.
Register and Enqueue:
In the snippet given above as example, 2 scripts are enqueued and 2 are registered. When you register a script it is made available. When enqueued, the script is pulled to theme for use. Registering can be done without having to enqueue the snippet. If the script has to be loaded, it has to be enqueued. It is also easy to track the functions when they are registered. If you are going to enqueue instantly, then you can also skip registering the script.
This is an example to show how the scripts can be loaded for front end in WordPress.