• Resolved marashmallow

    (@marashmallow)


    My site is behind Cloudflare which conveniently provides a visitor’s country as a two-letter code in $_SERVER["HTTP_CF_IPCOUNTRY"].

    Is there a way to programmatically use this to override the default country setting of the phone field in ‘National’ mode?

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

    (@wpmudev-support7)

    Hi @marashmallow

    Hope you are doing well!

    We’ll need to verify with the team if the functionality can be achieved with a code snippet. I have escalated this request to our Second Level Support Team, so they can provide further help and check if this is possible.

    Please keep in mind the Second Level Support 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 on this matter.

    Kind regards

    Luis

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @marashmallow

    Hope you are doing fine!

    Our Second Level Support team has provided a custom code that can help assign the default country setting of the phone field.

    You’ll need to add a must-use plugin to your site’s wp-content/mu-plugins folder as explained in our documentation here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins, then add the following code provided to the plugin’s php file:

    <?php 
    
    add_filter(
    	'forminator_field_phone_markup',
    	function( $html, $id, $required, $placeholder, $value ) {
    
    		if ( ! empty( $_SERVER["HTTP_CF_IPCOUNTRY"] ) ) {
    			$formi_country_codes = array_keys( forminator_get_countries_list() );
    
    			if ( in_array( strtoupper( $_SERVER["HTTP_CF_IPCOUNTRY"] ), $formi_country_codes ) ) {
    				$html = preg_replace( '/(data-country=".*?")/s' , 'data-country="' . strtolower( esc_attr( $_SERVER["HTTP_CF_IPCOUNTRY"] ) ) . '"', $html );
    			}
    		}
    
    
    		return $html;
    	},
    	10,
    	5
    );

    Keep in mind though, the validation of the country code might not be 100% accurate. Forminator uses JavaScript libraries to map the country code which may not match with the ones provided by Cloudflare.

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

    Hope this information helps.

    Kind regards

    Luis

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @marashmallow ,

    We haven’t heard from you for several days now, so it looks like you don’t have more questions for us.

    Feel free to re-open this topic if needed.

    Kind regards
    Kasia

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Dynamic default country for phone field?’ is closed to new replies.