Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I looked at the included scripts in WordPress (wp_enqueue_script) and didnt find what I should include to use jQuery UI Autocomplete

I am using it in the admin area. Should I just include jQuery UI from Google CDN? I think it will conflict with WordPress's jQuery UI? It appears to work tho.

share|improve this question

2 Answers 2

The jQuery autocomplete is not come with WP package. So you have to download this component from jQuery UI and enqueue it for use. Using jQuery UI at Google CDN also works, but in my opinion, that's not very good, because you will load whole the jQuery UI lib while you need only one component.

This is the code I used to load jQuery autocomplete (in a plugin):

add_action( 'admin_menu', 'wpse_21556_admin_menu' );

function wpse_21556_admin_menu() {
    $page = add_management_page( 'Plugin', 'Plugin', 'manage_options', 'plugin-page-hook', 'wpse_21556_plugin_page' );

    add_action( "admin_print_styles-{$page}", 'wpse_21556_admin_print_styles' );
}

function wpse_21556_admin_print_styles() {
    // jQuery autocomplete
    wp_enqueue_style( 'jquery-ui-autocomplete', plugins_url( 'css/jquery-ui-1.8.2.custom.css', __FILE__ ) );
    wp_register_script( 'jquery-ui-autocomplete', plugins_url( 'js/jquery.ui.autocomplete.min.js', __FILE__ ), array( 'jquery-ui-widget', 'jquery-ui-position' ), '1.8.2', true );

    // Plugin script and style
    wp_enqueue_style( 'plugin-css', plugins_url( 'css/style.css', __FILE__ ) );
    wp_enqueue_script( 'plugin-js', plugins_url( 'js/script.js', __FILE__ ), array( 'jquery-ui-autocomplete' ), '1.1', true );
}
share|improve this answer
    
Note: both jQuery UI and the autocomplete plugin are included in modern WordPress packages. A large list of options (these included) are available here: codex.wordpress.org/Function_Reference/wp_enqueue_script –  Zachary Schuessler Jun 22 at 21:41

I'm just beginning to play around with registering scripts, adding my own etc. I believe it's OK to use the Google CDN as long as you deregister the WordPress version (only when viewing the website, not in the Admin area).

I believe the correct way to do this is to use the if ( !is_admin ()... way. This will ensure everything continues to work correctly in the Admin Area, but still allows you to take advantage of the Google CDN.

Here is an example:

if ( !is_admin() ){
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"), false, '1.3.2');
   wp_enqueue_script('jquery');
}

I believe that is how to achieve what you're asking, but I would like to hear some of the "Guru's" suggestions on this.

*EDIT:

I should have noticed that you are using this in the admin area since it's clearly stated in your question. Here is the way suggested in the WP Codex Function Reference/wp enqueue script:

<?php
function my_init_method() {
    if (!is_admin()) {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    }
}    
add_action('init', 'my_init_method');
?>
share|improve this answer
    
I am using it in the admin section tho –  JM at Work Jul 8 '11 at 2:37
    
My first answer wasn't 100% correct. I've recently learned to exclude js from the admin area you use: wp_register_script('custom_script', get_bloginfo('template_directory') '/js/custom_script.js',.......... I'm sorry if my comment was misleading. I'm still learning as the in's and outs. . So either method I've give should work. –  Jeremy Jared Jul 8 '11 at 3:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.