You must not return
the value, but alter the varialbe $link
if(strpos($link, 'http://') !== 0) {
$link = 'http://' . $link;
} //no else needed
Be aware that if your link begins with https://
it will also prepend the http://
and result in http://https://example.com
.
The better way to do this would be from here, adjusted to your needs:
In functions.php
:
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
Code for you:
$link = addhttp( get_field('advertisement_link') );
$ad_code="<a href="".$link.'" target="_blank">Test</a>';