Phone Number URL

This code converts a standard phone number into the URL for a clickable phone link.

<?php
/**
* Phone URL
* @author Bill Erickson
* @link http://www.billerickson.net/phone-number-url
*
* @param string $phone_number, ex: (555) 123-4568
* @return string $phone_url, ex: tel:5551234568
*/
function ea_phone_url( $phone_number = false ) {
$phone_number = str_replace( array( '(', ')', '-', '.', '|', ' ' ), '', $phone_number );
return esc_url( 'tel:' . $phone_number );
}
view raw functions.php hosted with ❤ by GitHub

Example

<?php
$phone = get_post_meta( get_the_ID(), 'be_phone_number', true );
echo '<p>Phone: <a href="' . ea_phone_url( $phone ) . '">' . $phone . '</a></p>';
view raw functions.php hosted with ❤ by GitHub
Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk