How can I include jQuery file into my plugin so I can use it on plugin page?
1 Answer
JQuery is a part of Wordpress core, so it is automatically there for you to use (i.e. you do not need to register it). However, you still need to enqueue it.
This can be done as follows:
function my_script() {
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'my_script');
This loads jQuery from Wordpress core, the jQuery files can be found at /wp-includes/js/jquery/jquery.js if you are interested.
Alternatively you can include jQuery as a dependency when you enqueue your own JavaScript file as follows (notice the 3rd argument):
wp_enqueue_script( 'my_script_handle', plugins_url(('js/some_script.js, __FILE__), array('jquery'), '1.0', true);
where some_script.js is your own JavaScript file which is assumed to be in the 'js' directory of your plugin in the example.
You can find out more about the wp_enqueue_script parameters on the Wordpress Developer Help.
-
Thanks. I also want to load jquery only on specific page, so can I just check if the $hook is that page, or how? Oct 3, 2018 at 10:13
-
1