Porchy Web Development

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

 

Join the newsletter

Subscribe to get the latest content by email.

Powered by ConvertKit. If you give me your email address, you may receive emails from me (JJ) about posts on this site. You can unsubscribe at any time.

Porchy Ltd is a company registered in England, no. 12035925

VAT Registration no. 331196421

Built with WPGraphQL and Gatsby

© 2019 - 2021