This post is part of the ClassicPress Plugin Development series in which I am going to look at both best practice for developing plugins and how I approach some requirements as well as some of the functions I commonly use.
jQuery itself is automatically loaded by ClassicPress so we don’t need to do anything to load this ourselves in a plugin; it is just our own jQuery script which we need to register and enqueue. There is two ways in which scripts can be loaded in a plugin; I will cover them both, but will note first of all that I typically use the second approach; there is an argument that the first approach is the “correct” one.
The first thing to do when you are loading a script is to register it. This is done using the wp_register_script
function which ClassicPress provides.
wp_register_script(string $handle, string|bool $src, string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false)
wp_register_script parameters
$handle (string) (Required) Name of the script. Should be unique.
$src (string|bool) (Required) Full URL of the script, or path of the script relative to the WordPress root directory. If source is set to false, script is an alias of other scripts it depends on.
$deps (string[]) (Optional) An array of registered script handles this script depends on. To load jQuery this should be set to array('jquery').
Default value: array()
$ver (string|bool|null) (Optional) String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version. If set to null, no version is added.
Default value: false
$in_footer (bool) (Optional) Whether to enqueue the script before