Without http://
in front, your code won’t work. You will need to add that infront. You can make use of parse_url
to add http://
to urls that does not have that.
To make this protocol agnostic, you can simply use //
You can try something like this
$urlStr="www.google.com";
$parsed = parse_url($urlStr);
if ( empty( $parsed['scheme'] ) ) {
$urlStr="//" . ltrim( $urlStr, "https://wordpress.stackexchange.com/" );
}
echo '<a href="' . $urlStr . '">External</a>';