Skip Navigation

[Resolved] How to add autocomplte to select field?

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 13 replies, has 3 voices.

Last updated by Christian Cox 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#919823

Is there any hook by which I can add autocomplte feature to select field option?

Like this: hidden link

#919861

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Is this for a View filter or a Form select input?

You have a number of options such as the one you linked to or select2 (hidden link) which we use ourselves in certain places.

You would need to follow the documentation for whichever solution you choose, which will likely involve initiating the library on your select input (for which you will need an appropriate ID or class-based selector to identify it).

You can add such code in the custom JS section of your View or Form like so...

( function( $ ) {
	$( document ).ready( function(){
		// Your code here

	});
})( jQuery );

That simply waits until the DOM is ready and then runs whatever code you include. Note that you can use $ for jQuery as normal.

#919865

Thank Nigel to reply.

I want this autocomplete feature in custom select field of post (in admin panel).

#920195

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

In that case you will need to

1. add your custom JS to instantiate the autocomplete library on your select field to a .js file in your child theme (either add your code to an existing .js file or create a new one)

2. on the admin post edit screens for the custom post type where you use this select field you should
- enqueue the files of your autocomplete library of choice
- enqueue the .js file where you added your custom JS above

I can't write the JS to add the autocomplete for you, but let me point you in the right direction when it comes to how to correctly enqueue the required files:

function tssupp_enqueue_cpt_admin( $hook_suffix ){

    $post_tyoe = 'cpt-slug'; // Edit for your post type

    if ( in_array( $hook_suffix, array('post.php', 'post-new.php') ) ){

        $screen = get_current_screen();

        if ( is_object( $screen ) && $post_type == $screen->post_type ){

        	// Enqueue the autocomplete library files

        	// Enqueue the custom .js file

        }
    }
}

add_action( 'admin_enqueue_scripts', 'tssupp_enqueue_cpt_admin' );

See https://developer.wordpress.org/reference/hooks/admin_enqueue_scripts/ and https://developer.wordpress.org/reference/functions/get_current_screen/

Also, see https://developer.wordpress.org/themes/basics/including-css-javascript/

#920291

Thank you Nigel for help.

I tried it with following code:

function tssupp_enqueue_cpt_admin( $hook_suffix ){
 
    $post_type = 'my-custom-post'; 
 
    if ( in_array( $hook_suffix, array('post.php', 'post-new.php') ) ){
 
        $screen = get_current_screen();
 
        if ( is_object( $screen ) && $post_type == $screen->post_type ){
 
            wp_enqueue_script( 'choosen_script', get_template_directory_uri(). '/js/choosen.js', array(), '1.0' );
	        wp_register_style( 'choosen_css', get_template_directory_uri() . '/js/choosen.css', array(), '1.0' );
            wp_enqueue_style( 'choosen_css' );
 
        }
    }
}
add_action( 'admin_enqueue_scripts', 'tssupp_enqueue_cpt_admin' );

function make_autocomplete() {
    echo '<script type="text/javascript">
	$(".wpt-form-select").chosen();
	</script>';
}
add_action('admin_footer', 'make_autocomplete');

But its not working at all.

The working example is here:
hidden link

Please, can you find what am I missing?

#920292

Thank you Nigel for help.

I tried it with following code:

function tssupp_enqueue_cpt_admin( $hook_suffix ){
 
    $post_type = 'my-custom-post'; 
 
    if ( in_array( $hook_suffix, array('post.php', 'post-new.php') ) ){
 
        $screen = get_current_screen();
 
        if ( is_object( $screen ) && $post_type == $screen->post_type ){
 
             wp_enqueue_script( 'choosen_script', get_stylesheet_directory_uri() . '/js/choosen.js', array( 'jquery' ), '', true );
	        wp_register_style( 'choosen_css', get_template_directory_uri() . '/css/choosen.css');
            wp_enqueue_style( 'choosen_css' );
 
        }
    }
}
add_action( 'admin_enqueue_scripts', 'tssupp_enqueue_cpt_admin' );

function make_autocomplete() {
    echo '<script type="text/javascript">
	$(".wpt-form-select").chosen();
	</script>';
}
add_action('admin_footer', 'make_autocomplete');

But its not working at all.

The working example is here:
hidden link

Please, can you find what am I missing?

#920366

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The obvious problem I see is that the library you are using is called "chosen" but you are enqueuing files named "choosen".

Fix that and see if it works.

#920647

I changed the file name to "choosen".

File is added as I checked in source code but not working.

#920658

I update the code with this:

function tssupp_enqueue_cpt_admin( $hook_suffix ){
  
    $post_type = 'post-type'; 
  
    if ( in_array( $hook_suffix, array('post.php', 'post-new.php') ) ){
  
        $screen = get_current_screen();
  
        if ( is_object( $screen ) && $post_type == $screen->post_type ){
  
            wp_enqueue_script( 'choosen_script', get_stylesheet_directory_uri() . '/js/choosen.js', array(), '1.0' );
	wp_register_style( 'choosen_css', CHILD_URL . '/js/choosen.css');
    wp_enqueue_style( 'choosen_css' );
  
        }
    }
}
add_action( 'admin_enqueue_scripts', 'tssupp_enqueue_cpt_admin' );
 
function make_autocomplete() {
    echo '	
	<script type="text/javascript">
    $(".wpt-form-select").chosen();
    </script>';
}
add_action('admin_footer', 'make_autocomplete');

Its start working, but only in one select box that is on the top. How to make it that works for a specific select box, I means is there any way by which I can add a class to that select box?

I am using this for select box in repeatable field group.

#920864

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-07-04 at 14.35.16.png

You don't have any control over the generated markup in the backend, you can't add a wrapper div or anything like that, so you will just need to use a little detective work to come up with a suitable selector that targets all of the select fields you want.

In the screenshot you can see that my repeatable field group is inside a div with id="wpcf-group-project-task-fields" which is the custom field group in question, so I could try something like

jQuery('#wpcf-group-project-task-fields .wpt-form-select');

But I think you are going to find the problem is that this will work when editing existing fields, but if you add a new repeatable field group then the select field won't exist if you instantiated choose.js on DOM ready, for example.

So then you are getting into adding event listeners to the button to add a new repeatable field group and then instantiating choose.js on the newly created select field.

If this is starting to get too complex you may need to enlist the help of a developer, such as those listed in our contractors page: https://toolset.com/contractors/

#921066

Thank you Nigel.

Okay, when I am adding following code:

function add_autocomplete_feature( $hook ) {
    wp_enqueue_script( 'choosen_script', get_stylesheet_directory_uri() . '/js/choosen.js', array(), '1.0' );
	wp_register_style( 'choosen_css', CHILD_URL . '/css/choosen.css');
    wp_enqueue_style( 'choosen_css' );
}
add_action( 'admin_enqueue_scripts', 'add_autocomplete_feature' );

function make_autocomplete() {
    echo '<script type="text/javascript">
             $("select").chosen();
	      </script>';
}
add_action('admin_footer', 'make_autocomplete');

It means it must work on all select box in admin panel, and its working for all select box in admin but not working on repeatable field group or nested repeatable field. Can you please examine why?

#921313

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Make sure that the files are correctly enqueued.

On the edit page in the admin screen open up your browser tools and check that the chosen js and css files have been added to the page. (If you are using Chrome you'll find them in the application tab).

Your script should wait for DOM ready before trying to instantiate chosen on the select fields.

The script should be something like this:

( function( $ ) {
	$( document ).ready( function(){

		$('select').chosen();
	});
})( jQuery );

Check for errors in the browser console in case you are doing something wrong.

#922384

Thank you Nigel.

I checked the code as you suggested to me, files are correctly enqueued as I checked in source code (Ctrl+U).

Its working for all select box except, select box in repeatable field group or select box nested repeatable field group.

Can you please examine why is this happening?

#922646

It's happening because these select fields are not in the DOM when the document "ready" event is triggered. This means that your chosen initialization script is not run on these fields because RFG contents are added with AJAX after the page initially loads. Unfortunately I do not know a public event related to the AJAX load that is available, so there's not a good way to initialize these injected select fields after document.ready. You could add a setTimeout function that triggers the code after a specified amount of time, but it's not guaranteed that the fields will be ready yet.