Last active
November 27, 2022 13:02
-
-
Save yuriinalivaiko/98c85a04fdbd1b90308003e71081b87f to your computer and use it in GitHub Desktop.
This code snippet can fix a dropdown functionality in Ultimate Member if it is broken due to plugins conflict.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Re-initialize dropdown functionality. | |
* Add this code to your custom JS file. | |
*/ | |
jQuery( function () { | |
/** | |
* Verifies that there is no empty value. | |
*/ | |
function unselectEmptyOption( e ) { | |
var $element = jQuery( e.currentTarget ); | |
var $selected = $element.find( ':selected' ); | |
if ( $selected.length > 1 ) { | |
$selected.each( function ( i, option ) { | |
if ( option.value === '' ) { | |
option.selected = false; | |
$element.trigger( 'change' ); | |
} | |
} ); | |
} | |
}; | |
/** | |
* Re-initialize dropdown functionality. | |
*/ | |
function reInitializeDropdownFields() { | |
if ( 'function' === typeof jQuery.fn.select2 ) { | |
// Remove old dropdowns. | |
jQuery( '.um-s1, .um-s2, .um-s3', '.um-form' ).filter('.select2-hidden-accessible').select2('destroy').off('select2:select').siblings('span.select2').remove(); | |
// Initialize new dropdowns. | |
jQuery( '.um-s1' ).each( function ( e ) { | |
var obj = jQuery( this ); | |
obj.select2( { | |
allowClear: true, | |
dropdownParent: obj.parent() | |
} ).on( 'change', unselectEmptyOption ); | |
} ); | |
jQuery( '.um-s2' ).each( function ( e ) { | |
var obj = jQuery( this ); | |
if ( obj.parents( '.um-custom-shortcode-tab' ).length ) { | |
var atts = { | |
allowClear: false | |
}; | |
} else { | |
var atts = { | |
allowClear: false, | |
minimumResultsForSearch: 10, | |
dropdownParent: obj.parent() | |
}; | |
} | |
obj.select2( atts ).on( 'change', unselectEmptyOption ); | |
} ); | |
jQuery( '.um-s3' ).each( function ( e ) { | |
var obj = jQuery( this ); | |
obj.select2( { | |
allowClear: false, | |
minimumResultsForSearch: -1, | |
dropdownParent: obj.parent() | |
} ).on( 'change', unselectEmptyOption ); | |
} ); | |
} | |
} | |
/** | |
* Re-initializes dropdown functionality on the page load with timeout. | |
* Advice: Try to enlarge a timeout if the issue is not solved. | |
*/ | |
setTimeout( reInitializeDropdownFields, 99 ); | |
/** | |
* Re-initializes dropdown functionality on the Elementor popup open. | |
* This part in needed for the Elementor popup only. | |
*/ | |
jQuery( document ).on( 'elementor/popup/show', function () { | |
jQuery( window ).trigger( 'resize' ); | |
reInitializeDropdownFields(); | |
} ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a part of the article Fix dropdown field functionality
Documentation: https://docs.ultimatemember.com/
Support forum: https://wordpress.org/support/plugin/ultimate-member/