Using Select2 with ajax with CMB2 repeating groups


No idea if the title makes sense. I wanted a Select2 which uses a remote data source queried via ajax with a CMB2 repeating group so made a plugin to understand it better. It’s pretty heavily commented so I don’t forget why I did what I did. There are definitely a few if not many ways to do this, this is not the definitive guide.

The details are in the plugin, you should look at that if that’s what you’re after. Some interesting bits:

  • finding a data source which searched fast enough and was interesting enough was hard so I ended up using Select2’s example of Github
  • rather reasonably, Github has rate limits for unauthenticated requests which forced me to stop making so many damn requests (a very good thing which I should have done initially)
  • you can’t search The Wirecutter’s posts via its API which is understandable but if you try to use the Wirecutter be prepared to spend a while browsing the site and deciding you need a growler even though you had no clue what one was two seconds before and don’t drink anyway (maybe that’s just me)
  • there’s a plugin that adds a Select2 field to CMB2 but I don’t think handles ajax but if you know what your option list is going to be it looks good

 

2 responses to “Using Select2 with ajax with CMB2 repeating groups”

  1. sebastianvillar2016

    Hi! I’m looking for a wordpress plugin that works with cmb2 and ajax to load related custom post types. Do you think that this plugin will work?

  2. I think you could use it as a starting point but would need to adapt it for your use case. I do have something based on this running in production so it seems to work well enough!

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting this comment, you are agreeing to the use of Akismet which helps reduce spam. You can view Akismet’s privacy policy here. Your email, website and name are also stored on this site.