1

My code is as below:

<?php   
if( get_field( "facebook" ) !== '' ): ?>
<a href="<?php echo the_field('facebook'); ?>">Facebook</a>
<?php endif;?>

Instead of echoing the field's value which is (wwww.facebook.com), it's echoing it relative to the wordpress website.

Also, is my code efficient? Or is there a simpler way to do it?

Edit: What finally worked for me:

<?php
$website =  (get_field('website'));
if(!empty($website)){
$final_url = (!preg_match("~^(?:f|ht)tps?://~i", $website))? 'http://'.$website: $website;
echo "<a href=\"$final_url\">$final_url</a>" . "<br />";
}
?>  

1 Answers1

2

you should add http:// on the beggining to make external URLS

<a href="http://<?php echo the_field('facebook'); ?>">Facebook</a>

or add http:// on your advanced custom field in the admin

EDIT:

here is your final code:

$url = the_field('facebook');

if($url!=""){
    $final_url = (!preg_match("~^(?:f|ht)tps?://~i", $url))? 'http://'.$url: $url;
    echo '<a href="'.$final_url.'">Facebook</a><br/>';
}

NOTE:

  • your data wwww.facebook.com has excess w
  • i appended the code given by @feeela so it would check if http:// is present, thanks to @feeela
reikyoushin
  • 1,993
  • 2
  • 24
  • 40
  • …and see also [How to add http:// if it's not exists in the URL?](http://stackoverflow.com/questions/2762061/how-to-add-http-if-its-not-exists-in-the-url) – feeela May 03 '13 at 18:51
  • Well, if you can't decide in the application whether to link to another website or to your own, you may have a problem. – feeela May 03 '13 at 18:57
  • How can I add a line break only if the custom field is present? All that I have tried so far adds space regardless of whether the custom field is empty or filled. –  May 03 '13 at 19:37
  • 1
    @feeela: yeah i realized it should always be external given this situation (facebook link). you are right – reikyoushin May 03 '13 at 19:46
  • @reikyoushin, sorry if I'm being a total noob here. But I've started learning PHP 2 weeks ago. –  May 03 '13 at 19:51
  • no problem. is it working now or are there other problems you are encountering? – reikyoushin May 03 '13 at 19:52
  • I've just edited the code above. I'm trying to add the
    tag only if the custom field is processed. Otherwise, to skip to the next argument.
    –  May 03 '13 at 19:54
  • I think my main problem is with the name, address, phone, etc fields. They're passing a
    tag regardless of whether the field is there or not. I'd appreciate any general notes on how to make the above code more efficient. Is it bad style to keep using "echo.. "?
    –  May 03 '13 at 20:04
  • you're using advanced custom fields right? you should set the text field's formatting to `None` instead of `HTML`. you would see that on the plugin so it will not add auto-line breaks, see [screenshot](http://oi39.tinypic.com/33c436e.jpg) – reikyoushin May 03 '13 at 20:12
  • I'm using advanced custom fields. Changed the formatting to none. The problem is, even if the field is empty, the line break from IF statement is echoed: http://tinypic.com/view.php?pic=25u7h52&s=5. –  May 03 '13 at 20:55
  • i would need to have a source code for that besides the image.. though. – reikyoushin May 03 '13 at 21:02
  • and what does `var_dump(the_field('facebook'));` output? – reikyoushin May 03 '13 at 21:13
  • @reikyoushin Thanks for all your help! I've ended up using !empty, the code that finally worked for me is posted above as an edit. –  May 03 '13 at 22:43