using wp_enqueue_script to attach jquery-ui

Here’s an alternative way to load a popular script like jquery-ui (using google api’s)

<?php  
// first, check to see if jquery-ui is already loaded 
if( !wp_script_is('jquery-ui') ) { 
        // you don't have to use googleapi's, but I think it helps. It saves the user's browser from loading the same script again if it has already been loade>
    wp_enqueue_script( 'jquery-ui' , 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js' );
}   
?>  

*If you are still having problems, try using the full path to the “siteroot”, which is usually the directory you will find the wp-content folder in. So to load jquery-ui locally from your theme folder if it’s not connecting, try something like:

    <?php  
// first, check to see if jquery-ui is already loaded 
if( !wp_script_is('jquery-ui') ) { 
    wp_enqueue_script( 'jquery-ui' , '/home/<myusername>/<mysite.com>/wp-content/themes/<mycustomthemename>/js/jquery-ui.min.js' );
}   
?>  

This only works if you have the file in that folder and inside of a js folder you created inside the theme. This is inadvisable if you need to change sites to a different url – but you will just have to update the folder if you do so.

Leave a Comment