Support

Account

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

Solving

Select2 not working

    • Robert

    • December 16, 2014 at 8:45 pm

    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?

    • Robert

    • December 18, 2014 at 8:05 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.