Created
May 30, 2026 07:56
-
-
Save pramodjodhani/67732c9e33baba4b87634b70d6e6b113 to your computer and use it in GitHub Desktop.
Multi-currency for GF: show the currency dropdown as a button group.
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
| <?php | |
| /** | |
| * Multi-currency for GF: show the currency dropdown as a button group. | |
| * | |
| * Requires: Gravity Forms + Multi-currency for Gravity Forms. | |
| * Paste into Code Snippets (run everywhere) or your theme functions.php. | |
| */ | |
| add_action( 'wp_enqueue_scripts', 'idea_mcg_currency_switcher_button_group_assets', 20 ); | |
| function idea_mcg_currency_switcher_button_group_assets() { | |
| if ( is_admin() ) { | |
| return; | |
| } | |
| $css = ' | |
| .ginput_container_multicurrency .mcg-currency-select-hidden { | |
| position: absolute !important; | |
| width: 1px !important; | |
| height: 1px !important; | |
| padding: 0 !important; | |
| margin: -1px !important; | |
| overflow: hidden !important; | |
| clip: rect(0, 0, 0, 0) !important; | |
| white-space: nowrap !important; | |
| border: 0 !important; | |
| } | |
| .mcg-currency-button-group { | |
| display: flex; | |
| flex-wrap: wrap; | |
| } | |
| .mcg-currency-button-group .mcg-currency-btn { | |
| cursor: pointer; | |
| padding: 0.5rem 1rem; | |
| border: 1px solid #ccc; | |
| border-radius: 0 !important; | |
| background: #fff !important; | |
| font: inherit; | |
| line-height: 1.2; | |
| color: #000 !important; | |
| outline: none !important; | |
| } | |
| .mcg-currency-button-group .mcg-currency-btn:focus { | |
| outline: none !important; | |
| } | |
| .mcg-currency-button-group .mcg-currency-btn:hover { | |
| border-color: #666; | |
| } | |
| .mcg-currency-button-group .mcg-currency-btn.is-active { | |
| background: #2271b1 !important; | |
| border-color: #2271b1 !important; | |
| color: #fff; | |
| } | |
| .mcg-currency-button-group .mcg-currency-btn:focus-visible { | |
| outline: 2px solid #2271b1; | |
| outline-offset: 2px; | |
| } | |
| '; | |
| wp_register_style( 'idea-mcg-currency-buttons', false ); | |
| wp_enqueue_style( 'idea-mcg-currency-buttons' ); | |
| wp_add_inline_style( 'idea-mcg-currency-buttons', $css ); | |
| wp_register_script( 'idea-mcg-currency-buttons', false, array( 'jquery' ), '1.0.0', true ); | |
| wp_enqueue_script( 'idea-mcg-currency-buttons' ); | |
| $js = <<<'JS' | |
| (function ($) { | |
| function syncActiveButton($select) { | |
| var $group = $select.siblings('.mcg-currency-button-group'); | |
| if (!$group.length) { | |
| return; | |
| } | |
| $group.find('.mcg-currency-btn').removeClass('is-active').filter(function () { | |
| return $(this).data('currency') === $select.val(); | |
| }).addClass('is-active'); | |
| } | |
| function initCurrencyButtonGroups(context) { | |
| $(context || document).find('.ginput_container_multicurrency select').each(function () { | |
| var $select = $(this); | |
| if ($select.closest('.gform_editor').length || $select.is(':disabled')) { | |
| return; | |
| } | |
| if ($select.siblings('.mcg-currency-button-group').length) { | |
| syncActiveButton($select); | |
| return; | |
| } | |
| var $group = $('<div class="mcg-currency-button-group" role="group"></div>'); | |
| $select.find('option').each(function () { | |
| var code = $(this).val(); | |
| var label = $(this).text().trim() || code; | |
| var $btn = $('<button type="button" class="mcg-currency-btn"></button>') | |
| .attr('data-currency', code) | |
| .text(label); | |
| if ($select.val() === code) { | |
| $btn.addClass('is-active'); | |
| } | |
| $btn.on('click', function () { | |
| if ($select.val() !== code) { | |
| $select.val(code).trigger('change'); | |
| } | |
| }); | |
| $group.append($btn); | |
| }); | |
| $select.addClass('mcg-currency-select-hidden').on('change.mcgButtonGroup', function () { | |
| syncActiveButton($select); | |
| }); | |
| $select.before($group); | |
| }); | |
| } | |
| $(document).ready(function () { | |
| initCurrencyButtonGroups(); | |
| }); | |
| $(document).on('gform_post_render gform_page_loaded', function (event, formId) { | |
| if (formId) { | |
| initCurrencyButtonGroups('#gform_wrapper_' + formId); | |
| } else { | |
| initCurrencyButtonGroups(); | |
| } | |
| }); | |
| })(jQuery); | |
| JS; | |
| wp_add_inline_script( 'idea-mcg-currency-buttons', $js ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment