HubSpot HubL: Handling All URL Types in a Button Module Using URL Field


HubSpot HubL: Handling All URL Types in a Button Module Using URL Field

When building HubSpot modules, handling different URL types correctly is essential for a seamless user experience. HubSpot’s URL field supports multiple types, including external links, internal content, files, emails, phone numbers, and even WhatsApp. To ensure your button links work as expected across all these types, you can use the following HubL code snippet. This snippet dynamically adjusts the href attribute based on the URL type while also handling special cases like mailto: for emails and tel: for phone numbers.

For more details on HubSpot’s URL field and its supported types, check out HubSpot’s documentation. 🚀

  
{% set button_href = module.button.url_field.href_with_scheme|escape_url %}
        {% set popup_class = "" %}
        {% set new_tab = false %}

        {% if module.button.url_field.type == "EMAIL_ADDRESS" %} 
          {% set button_href = "mailto:" ~ module.button.url_field.href %}
        {% elif module.button.url_field.type == "PHONE_NUMBER" %}
          {% set button_href = "tel:" ~ module.button.url_field.href %}
        {% elif module.button.url_field.type == "WHATSAPP_NUMBER" %}
          {% set button_href = "https://wa.me/" ~ module.button.url_field.href|replace("+", "")|escape_url %}
        {% elif module.button.url_field.type == "FILE" or module.button.url_field.type == "EXTERNAL" %}
          {% set new_tab = true %}
        {% elif module.button.url_field.type == "CONTENT" or module.button.url_field.type == "BLOG" %}
          {% set button_href = module.button.url_field.href|escape_url %}
        {% else %}
          {% set button_href = module.button.url_field.href|escape_url %}     
        {% endif %}

        <a class="{{ module.button.css_classes }}{% if popup_class %} {{ popup_class }}{% endif %}" href="{{ button_href }}" target="_blank" rel="noopener">
          {{ module.button.text }}
        </a>      

About the Author

Jake Lett is a results-driven Detroit based B2B marketing consultant with 15+ years of hands-on experience managing SEO and PPC campaigns across manufacturing, SaaS, and professional services industries. He’s a Certified Google Ads Specialist and HubSpot CMS Developer who has personally managed budgets ranging from $500 to over $10,000/month.

Jake specializes in helping small businesses and solo marketers get more from lean ad budgets—using practical strategies that drive qualified leads, not just traffic. He shares real-world lessons on his blog, YouTube channel, and in his published books on digital marketing.



Related posts

Tags: , , ,

Want to Get Email Updates of New Articles?

Join My Email Newsletter