Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active January 15, 2024 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/ada52f36f95c3c06d022aebd3a467d62 to your computer and use it in GitHub Desktop.
Save damiencarbery/ada52f36f95c3c06d022aebd3a467d62 to your computer and use it in GitHub Desktop.
Use Select2 for Ninja Forms dropdowns - When a Ninja Forms form has a dropdown, enable Select2 so that the elements are searchable. https://www.damiencarbery.com/2023/12/use-select2-for-ninja-forms-dropdowns/
// This code enables Select2 to dropdowns as they are rendered.
// Create a new object for custom validation of a custom field.
var dcwdDropdownFieldController = Marionette.Object.extend( {
initialize: function() {
// From: https://developer.ninjaforms.com/codex/field-view-rendering/
this.listenTo( nfRadio.channel( 'listselect' ), 'render:view', this.renderView );
},
renderView: function( view ) {
if ( -1 != view.model.get('container_class').search( 'enable-select2' ) ) {
var el = jQuery( view.el ).find( '.nf-element' );
//console.log( 'nf-element', el );
el.select2();
}
},
});
jQuery( document ).ready( function( $ ) {
// Instantiate our custom field's controller, defined above.
new dcwdDropdownFieldController();
});
<?php
/*
Plugin Name: Use Select2 for Ninja Forms dropdowns
Plugin URI: https://www.damiencarbery.com/2024/01/use-select2-for-ninja-forms-dropdowns/
Description: When a Ninja Forms form has a dropdown, enable Select2 so that the elements are searchable. Will work with Ninja Forms Conditional Logic.
Author: Damien Carbery
Version: 0.5
*/
defined( 'ABSPATH' ) || exit;
// If the form has a dropdown include Select2 JS and CSS and the custom JS.
add_filter( 'ninja_forms_render_options', 'dcwd_nf_forms_check_for_dropdown', 10, 2 );
function dcwd_nf_forms_check_for_dropdown( $options, $settings ) {
// Verify that the dropdown container has the 'enable-select2' class.
if ( isset( $settings['container_class'] ) && false !== strpos( $settings['container_class'], 'enable-select2' ) ) {
add_action( 'wp_footer', 'dcwd_custom_select2_css' );
wp_enqueue_style( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css', array(), null );
wp_enqueue_script( 'select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js', array('jquery'), null, array( 'in_footer' => false, ) );
wp_enqueue_script( 'dropdown-custom', plugins_url( 'enable-select2-on-ninja-forms-dropdowns.js', __FILE__ ), array('nf-front-end'), null, array( 'in_footer' => true, ) );
}
return $options;
}
function dcwd_custom_select2_css() {
// Override Ninja Forms CSS to allow the select2 element to be seen (otherwise
// it is hidden behind the original select field).
?>
<style>
.nf-form-content .enable-select2 .list-select-wrap > div div { display: none; }
</style>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment