Well, I know this is a commonly asked question, but almost all of them like '2 or 3 years ago'. Since Wordpress had some serious core changes recently, thought it'd be better to get an updated answer.
What I wanted to do is add this jquery slider to my wordpress custom admin page.
In today's Wordpress, is jquery-ui coming in built ? or I do need to include it using wp_enqueue_script
or I should use some external url CDN?
Since I'm working on Wordpress 4.2, could you please tell me the proper way of adding this functionality?
Thanks!
jQuery-ui actually ships with WordPress. So since you are working in WordPress, this is how you should add jquery ui ( the slider ) to your application.
1)
your-script-name
is the name of your script.2) get_template_directory_uri() gets the url to your theme. If you are using the above code in a plugin then use plugin_dir_url( FILE ).
https://codex.wordpress.org/Function_Reference/plugin_dir_url
3) array('jquery', 'jquery-ui-slider') is the list of dependencies that your js app requires. In this case it needs
jquery
andjquery-ui-slider
.4)
1.0.0
is the version of your js application.5)
true
means you are outputting your application in the footer of the admin page.More reference on
wp_enqueue_script
on https://codex.wordpress.org/Function_Reference/wp_enqueue_scriptexample:
$('#menu')
in wp:jQuery('#menu')
Yes the wordpress allows you to add multiple dependencies. You can check the details on the wp_enqueue_script() page in the documentation.
What you'd do is add a dependency to your script where you initialize your slider. In your
functions.php
where you enqueue your scripts add thisexample.js
is the script file where you initialize your slider. It depends explicitly onjquery
and onjquery-ui-slider
, so you know that the slider will work with the slider ui component of the jquery-ui.It's split into parts so that you don't have to enque entire
jquery-ui
which is quite heavy on the page.