Trying to check if $link
has http://
in front of it or not. If someone puts in www.google.com
for the link field it acts as a link within the WordPress site, ie: www.website.com/www.google.com
.
$link = get_field('advertisement_link');
$ad_code = '<a href="'.$link.'" target="_blank">Test</a>';
Sample code above I am working with. This is a general PHP question as well. How do I do this the right way? Tried below, replaces the_content()
with the $link
.
$link = get_field('advertisement_link');
if(strpos($link, 'http://') !== 0) {
return 'http://' . $link;
} else {
return $link;
}
$ad_code = '<a href="'.$link.'" target="_blank">Test</a>';
Edit: I need to keep $ad_code
because I'm using it inside return prefix_insert_after_paragraph( $ad_code, 3, $content );
to insert this ad into a post every 3 paragraphs.