Phone Number URL

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

/**
 * Phone URL 
 * @author Bill Erickson
 * @link https://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 );
}

Example

$phone = get_post_meta( get_the_ID(), 'be_phone_number', true );
echo '<p>Phone: <a href="' . ea_phone_url( $phone ) . '">' . $phone . '</a></p>';
Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk