How to use in my plugin the Autocomplete for users and sites field?

i am building plugin for my multisite, so i need to be able to select some blog from the blogs list, and then get the blog id to use this id in another function.

the best solution will be using Autocomplete for users and sites field, the same wordpress using in Network admin.

the question is how i can use the same field in my plugin?

after i have start searching for solution i have found this topic on wordpress: https://core.trac.wordpress.org/attachment/ticket/19810/19810.21.patch

but i still did not realize how to use this in my plugin.

Thanks!

  • Ruslan

    Hi @Jack Kitterhing
    here what i need.
    if the first screenshot we have all the "sites" in our multisite.
    there is column named "domains".
    so what i need is to create field that will Autocomplete me when i will start to typing some of the domain name. for example:
    test: it will show me test domain and all others domains if exist that contains "test"
    the best example is the "user field as shown in the screenshot two.
    when you start typing username the field Autocomplete you.

    so i want to make the same field for domains.

    Thanks

  • calvinrogercanas

    Hello @Ruslan

    Hope you're well today! :slight_smile:

    You want to have autocomplete feature on your plugin so that users can easily select or search the element you want.

    As I don't have experience creating plugin that have autocomplete feature but I know generally speaking what you want to achieve.

    You need to have knowledge of jQuery and Ajax to make this. You also need to know how to query the data you want ( I assume you want to query all available sites in your multisite. ) to search and fetch it thru AJAX method so you will get real-time results and don't need to enter Search Button just to see the results. Example of an autocomplete plugin: http://jqueryui.com/autocomplete/

    Looking forward for your reply and I hope you can create the plugin you need. :slight_smile:

    Best Regards,
    Calvin

  • Ruslan

    Hi,
    it almost work but i seems i can get data from the DB to autocomplete field.

    i have create field:

    <form method="post">
    <input id="mytest" type="text" name="mytest" style="width: 300px" />
     </form>

    i have set Jquery code:

    (function( $ ) {
    	$(function() {
            $( "#mytest" ).autocomplete({
                source: "<? echo $campaign_plugin_url; ?>/autocomplete.php",
                minLength: 1 // how many character when typing to display auto complete
            });
    	});
    
    })( jQuery );

    here is the code(in autocomplete.php) to get blogs from DB (this code was in wordpress core until 3.4):

    function wp_ajax_autocomplete_site() { 
    
    	 	    global $wpdb;
    	        $like_escaped_term = strtolower( $_GET['term'] );
     	        $sites = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE ( domain LIKE %s OR path LIKE %s ) AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $like_escaped_term, $like_escaped_term ), ARRAY_A ); 
    
     	        if ( empty( $sites ) )
    	 	                wp_die( -1 ); 
    
     	        foreach ( (array) $sites as $details ) {
    	 	                $blogname = get_blog_option( $details['blog_id'], 'blogname' );
    	                $return[] = array(
    	                        'label' => sprintf( '%1$s (%2$s)', $blogname, $details['domain'] . $details['path'] ),
     	                        'value' => $details['domain']
    	 	                );
     	        } 
    
     	       $response = json_encode( $return );
    			echo $response;
        	exit();
    }

    Jquery autocomplete works without any errors but don't get any data back.

    need your help!

    Thanks

  • Vinod Dalvi

    Hi @Ruslan

    Jquery autocomplete works without any errors but don't get any data back.

    Could you please tell me a bit more in detail what is working for you and where you are facing the problem so that i can help you to resolve it?

    Also would you mind if I logged in to your site and did some testing? This might help get to the bottom of this faster. If this is ok, just grant me temporary admin access to your site by clicking "Grant Access" button in the WPMU DEV Dashboard Settings as described on the following page and reply on this thread after granting it?

    https://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi @Ruslan,

    You can find all the autocomplete code that we have developed in our New Blog Templates plugin by downloading it from the following page.

    https://premium.wpmudev.org/project/new-blog-template/

    Developing custom code for the custom functionality is beyond the scope of support that we provide and i am sure that you understand this.

    If you are facing any difficulty in finding/developing the code then you can consider hiring a developer from https://premium.wpmudev.org/wpmu-custom-development/ to develop it for you.

    Any other issues, we're here to help :slight_smile:

    Thank you for being a WPMU DEV member and have a fantastic day!

    Cheers,
    Vinod Dalvi

  • Ruslan

    Hi @Vinod Dalvi,
    i don't want help in the development process. i have create my code but something to working.
    then i saw you have something similar in the "New Blog Templates plugin "
    i have check the code and found that all the code your using to get this work is ajax.php and nbt-templates.js.

    but it seems that i missing something else.
    so if you can give just the direction i will continue from there.

  • Vinod Dalvi

    Hi @Ruslan,

    I checked the code New Blog Templates plugin code and found the code should work fine from both the files ajax.php and nbt-templates.js if you have changed the selectors in the file nbt-templates.js with the appropriate selectors that you are using.

    Is the AJAX request getting sent?

    Are you not getting any output using this code or getting any error or anything else?

    I want to test it on your site so please grant us support staff access if you are using WPMU DEV Dashbaord on your development site or just send log in info through our secured contact form: https://premium.wpmudev.org/contact/
    - Choose "I have a different question"
    - Include my name in the subject "Vinod Dalvi"
    - Include the URL of this post in your message so that I may track this issue better
    - Include login information (Wordpress admin info username + password )
    - Include FTP Details

    Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.