• Resolved arturweb

    (@arturweb)


    By default, only US and UK is displayed on top of the dropdown list for phone field. How to add my own preferred countries for international mod selected?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @arturweb

    Hope you are doing fine!

    Currently, it is not possible to change the preferred countries out of the box. However, I have shared this request with our development team so they can evaluate if the list can be adjusted. Depending on the results of the evaluation and the complexity of this change, they will confirm if a workaround can be provided.

    Keep in mind the team works with more complex issues and their response may take some considerable time. Once they have an update, a notification will be sent and a post will also be added in the ticket thread.

    Thanks in advance for your patience.

    Kind regards

    Luis

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @arturweb

    Hope you are doing fine.

    The Second Level Support team helped with a custom code solution that should allow you to change the preferred countries.

    Please add a must-use plugin to your site’s wp-content/mu-plugins folder like this https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins, then add the following code to the plugin’s php file:

    <?php 
    
    add_action( 'wp_footer', 'wpmudev_fix_intelinput_country', 9999 );
    function wpmudev_fix_intelinput_country() {
    	global $post;
        if ( is_a( $post, 'WP_Post' ) && !has_shortcode($post->post_content, 'forminator_form') ) {
            return;
        }
    	?>
    	<script type="text/javascript">
    	jQuery( document ).ready( function($) {
    		setTimeout(function() {
    			$('.forminator-custom-form').trigger('after.load.forminator');
    		},100);
    
    		$(document).on('after.load.forminator', function(e, form_id) {
    			var form        = $( '#forminator-module-2910' ),
    				is_material = form.is('.forminator-design--material'),
    				fields      = form.find('.forminator-field--phone');
    			fields.each(function () {
    				$(this).intlTelInput('destroy');
    				var is_national_phone = $(this).data('national_mode'),
    					country           = $(this).data('country'),
    					validation        = $(this).data('validation');
    
    				if ('undefined' !== typeof (is_national_phone)) {
    
    					if (is_material) {
    						//$(this).unwrap('.forminator-input--wrap');
    					}
    
    					var args = {
    						nationalMode: ('enabled' === is_national_phone) ? true : false,
    						initialCountry: 'undefined' !== typeof ( country ) ? country : 'us',
    						utilsScript: window.ForminatorFront.cform.intlTelInput_utils_script,
    					};
    
    					if ( 'undefined' !== typeof ( validation ) && 'standard' === validation ) {
    						args.allowDropdown  = false;
    					}
    					// stop from removing country code.
    					if ( 'undefined' !== typeof ( validation ) && 'international' === validation ) {
    						args.autoHideDialCode = false;
    					}
    
    					args.preferredCountries = ["al", "us", "gb"]; //These countries will appear at the top
    
    					$(this).intlTelInput(args);
    
    					if ( 'undefined' !== typeof ( validation )
    						&& 'international' === validation ) {
    						var dial_code = $(this).intlTelInput( 'getSelectedCountryData' ).dialCode,
    							country_code = '+' + dial_code;
    						if ( country_code !== $(this).val() ) {
    							var phone_value = $(this).val().trim().replace( dial_code, '' );
    								$(this).val( country_code + phone_value );
    						}
    					}
    
    					if ( ! is_material ) {
    						$(this).closest( '.forminator-field' ).find( 'div.iti' ).addClass( 'forminator-phone' );
    					} else {
    						$(this).closest( '.forminator-field' ).find( 'div.iti' ).addClass( 'forminator-input-with-phone' );
    
    						if ( $(this).closest( '.forminator-field' ).find( 'div.iti' ).hasClass( 'iti--allow-dropdown' ) ) {
    							$(this).closest( '.forminator-field' ).find( '.forminator-label' ).addClass( 'iti--allow-dropdown' );
    						}
    					}
    
    					// intlTelInput plugin adds a markup that's not compatible with 'material' theme when 'allowDropdown' is true (default).
    					// If we're going to allow users to disable the dropdown, this should be adjusted accordingly.
    					if (is_material) {
    						//$(this).closest('.intl-tel-input.allow-dropdown').addClass('forminator-phone-intl').removeClass('intl-tel-input');
    						//$(this).wrap('<div class="forminator-input--wrap"></div>');
    					}
    				}
    			});
    		});
    	});
    	</script>
    	<?php
    }

    Important:

    • Please change the 2910 number to your form id in the following line of the code:
      var form = $( ‘#forminator-module-2910’ ),
    • Add the preferred countries in this line of code, use the 2 letter code of the country:
      args.preferredCountries = [“al”, “us”, “gb”]; //These countries will appear at the top

    We recommend to test this on the dev/staging version first before putting it on the live site.

    Hope it will solve your problem.

    Kind Regards

    Luis

    Thread Starter arturweb

    (@arturweb)

    @wpmudev-support7 thanks! it works for me

    I have only one question: if I have several forms, do I need to duplicate this function for each one?

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @arturweb

    I’ve asked our developers for advice on this. we’ll update you here once we get solution from them.

    Best regards,
    Adam

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @arturweb

    I just got response from our developers with modified code. You’ll find a new version of it here:

    https://pastebin.com/WLL7s3CK

    Note: you will need to add all the forms IDs in this line

    if ( e.target.id == 'forminator-module-2910' || e.target.id == 'forminator-module-3210' ) {

    Above line is for two forms but if you want o add more, you just need to follow the patter. For example, for three forms it would be like

    if ( e.target.id == 'forminator-module-2910' || e.target.id == 'forminator-module-3210' || e.target.id == 'forminator-module-4569') {

    I hope that makes some sense 🙂

    Best regards,
    Adam

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @arturweb ,

    We haven’t heard from you for 4 days now, so it looks like there are no more questions for us.
    
    Feel free to re-open this topic if needed.

    Kind regards
    Kasia

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Ho to change preferred countries in the international phone field?’ is closed to new replies.