Add country option for WhatsApp and Telegram

Hey HivePress team,

I’ve found a minor ‘bug’ that would be great if you’re able to help fix!

Under HivePress > Settings > on the Listings & Vendors tabs, under ‘Social Links’ I have enabled WhatsApp. However, unlike when I add a phone number ‘attribute’; I’m unable to select the ‘Default Country’.

While I was using another theme, I found I was able to ‘fix’ the issue with a custom script, but since reverting to using Listing Hive my ‘fix’ seems to have stopped working no matter where I place it.

I’d guess this is only a very small issue for you guys to fix, so hopefully you can push out a quick update soon. I’ve attached the snippet I was using previously below in case it’s of any use to anyone:

<script>
document.addEventListener('DOMContentLoaded', function() {
    function setPhoneCountry() {
      const phoneInput = document.querySelector('input[data-intl-tel-input-id="1"]');
      if (phoneInput && typeof window.intlTelInput !== 'undefined') {
        let iti = window.intlTelInputGlobals.getInstance(phoneInput);
        if (!iti) {
          iti = window.intlTelInput(phoneInput, {
            initialCountry: 'gb',
            utilsScript: phoneInput.getAttribute('data-utils')
          });
        } else {
          iti.setCountry('gb');
        }
        return true;
      }
      return false;
    }
    if (!setPhoneCountry()) {
      let attempts = 0;
      const interval = setInterval(function() {
        if (setPhoneCountry() || attempts >= 10) {
          clearInterval(interval);
        }
        attempts++;
      }, 500);
    }
  });
  </script>

Cheers,
Chris :victory_hand:

Thanks for reporting the issue! Another possible PHP fix is using the hivepress/v1/models/listing/attributes or hivepress/v1/models/vendor/attributes hook (depends on if links are enabled for listings or vendors) and setting the “country” parameter to the country code for the needed field.

Many thanks for the tip @ihor, though I’m not really a developer, so I’m not sure how to put it all together. I asked AI for help and it gave me this, but it doesn’t seem to be working:

add_filter(
    'hivepress/v1/models/vendor/attributes',
    function( $attributes ) {
        if ( isset( $attributes['whatsapp'] ) ) {
            $attributes['whatsapp']['edit']['country'] = 'GB';
        }
        return $attributes;
    }
);
add_filter(
    'hivepress/v1/models/listing/attributes',
    function( $attributes ) {
        if ( isset( $attributes['whatsapp'] ) ) {
            $attributes['whatsapp']['edit']['country'] = 'GB';
        }
        return $attributes;
    }
);

Much apprciated if you’re able to help tweak it as HivePress expects.

Cheers

The AI is pretty close, please try this snippet instead (for listings):

add_filter(
    'hivepress/v1/models/listing/attributes',
    function( $attributes ) {
        if ( isset( $attributes['whatsapp'] ) ) {
            $attributes['whatsapp']['edit_field']['country'] = 'GB';
        }
        return $attributes;
    }, 1000
);

For vendor links, change hivepress/v1/models/listing/attributes to hivepress/v1/models/vendor/attributes

Hope this helps

1 Like

That worked perfectly. Thank you so much, @ihor! :folded_hands:

Obviously, it’s not a huge deal, but it would be nice to see it implemented natively at some point. Otherwise, hopefully others find this topic useful! :slight_smile:

1 Like