Support

Account

Home Forums Backend Issues (wp-admin) Select2 not working

Solving

Select2 not working

  • Hi,

    Im working on a new plugin, and for some reason the Select2 select is not showing up.

    I didnt find any documentation about how to use Select2 in a plugin I could just read the Post Object field source code. I guess if im turning ui and ajax true it should be working automatically:

    $field['ui'] = 1;
    $field['ajax'] = 1;

    What can be the problem with my code? Its almost the same to Post Object. Should I use css or extra javascript?

    Thanks!

  • Is the select being generated from acf or from your own code? What’s the setup look like? Are there any JS errors in console?

  • Here is the whole code:

    // Change Field into a select
    $field['type'] = 'select';
    $field['ui'] = 1;
    $field['ajax'] = 1;
    $field['choices'] = array();
    
    // get sites
    $sites = $this->acf_get_sites();
    
    // set choices
    if( !empty($sites) ) {
    
    	foreach( $sites as $site ) {
    		
    		//get blog name
    		$current_blog_details = get_blog_details( array( 'blog_id' => $site['blog_id'] ) );
    		
    		// append to choices
    		$field['choices'][ $site["blog_id"] ] = $current_blog_details->blogname;
    
    	}
    
    }
    
    // render
    acf_render_field( $field );

    Its almost the same like the post_object. I have no JS errors. Without ui and ajax its just fine.

  • @bokorir I’m trying to achieve almost the same result using select2: the main difference is that I’m querying an external source (MongoDB returning json data).

    I have copied most of the code from the taxonomy field and I looked at post_object, too.
    As you, if I specify
    $field['ui'] = 1;
    the select field is rendered in the HTML code but it remains hidden.
    If I remove the ‘ui’ setting or change it to 0, the select field is shown but it is disabled.

    Did you manage to solve this problem?

    Thanks,
    Roberto

  • It works by adding this script right after acf.add_action(‘ready append’, function( $el ){}

    // field_name
    	acf.fields.FIELD_NAME = acf.fields.select.extend({
    
    		type: 'FIELD_NAME',
    
    	});
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Select2 not working’ is closed to new replies.