Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

External url link from custom profile field WordPress

  • SOLVED

I am wanting to display a custom profile field called facebook on the front end if it is present...

<p><strong>Connect On Facebook:</strong><br /><a class="facebook" href="<?php echo $curauth->facebook; ?>">Facebook Wall</a>

Using the code above the url clickable link appends the domain name to the beginning of the link ie. http://www.domainname.com.au/www.facebook.com

where I am wanting it to go to www.facebook.com

Finally, if the custom profile field is empty then display the text <p>Empty</p>

So if custom profile field exists, display a clickable link that takes you to an external site
else if it is empty
display text stating it is empty

Any help would be greatly appreciated...

Thanks,
Sean.

Answers (2)

2014-01-04

Hariprasad Vijayan answers:

Hello,

You should use http://www.facebook.com while saving it on user profile. http:// is missing here

Or
You can add http:// if $curauth->facebook missing http://
The code for this is

if (strpos($curauth->facebook,'http://') === false){
$facebook_url = 'http://'$curauth->facebook;
} else {
$facebook_url = $curauth->facebook;
}

and use $facebook_url to display output like
<p>Connect On Facebook:<br /><a class="facebook" href="<?php echo $facebook_url; ?>">Facebook Wall</a><p>



And following code can use for display if field exists
<?php
if(!empty($curauth->facebook))
{?>
<p>Connect On Facebook:<br /><a class="facebook" href="<?php echo $curauth->facebook; ?>">Facebook Wall</a></p>
<?php } else {?>
<p>Empty</p>
<?php } ?>


Hariprasad Vijayan comments:

Let me know if you have any doubt in this


Hariprasad Vijayan comments:

Final answer

Use the following code

<?php
$facebook_url = $curauth->facebook;
if(!empty($facebook_url))
{
if(strpos($facebook_url,'http://') === false){
$facebook_url = 'http://'.$facebook_url;
} else {
$facebook_url = $facebook_url;
}
}
if(!empty($facebook_url))
{?>
<p>Connect On Facebook:<br /><a class="facebook" href="<?php echo $facebook_url; ?>">Facebook Wall</a></p>
<?php } else {?>
<p>Empty</p>
<?php } ?>


Sean Gartlan comments:

Thanks again Hari,

I have updated to the following;

Updating the input to include http:// has solved the url not being correctly resolved...

The only issue I have with the approach below is that if a custom field value does not exist then the heading i.e <p><strong>Twitter:</strong> does not display... If the empty logic is true I still need the heading to be returned... So it would look like;

<strong>Twitter:</strong>
Empty


<?php
if(!empty($curauth->tweet))
{?>

<p><strong>Twitter:</strong><br /><a class="tweet" href="<?php echo $curauth->tweet; ?>">Twitter</a></p>

<?php } else {?>
<p>Empty</p>
<?php } ?>

<?php
if(!empty($curauth->facebook))
{?>

<p><strong>Facebook:</strong><br /><a class="facebook" href="<?php echo $curauth->facebook; ?>">Facebook</a></p>

<?php } else {?>
<p>Empty</p>
<?php } ?>
<?php

if(!empty($curauth->linkedin))
{?>

<p><strong>Linkedin:</strong><br /><a class="linkedin" href="<?php echo $curauth->linkedin; ?>">Linkedin</a></p>

<?php } else {?>
<p>Empty</p>
<?php } ?>


Hariprasad Vijayan comments:

Just use like this


<?php
if(!empty($curauth->tweet))
{?>
<p>Twitter:<br /><a class="tweet" href="<?php echo $curauth->tweet; ?>">Twitter</a></p>
<?php } else {?>
<p>Twitter:<br />Empty</p>
<?php } ?>


Hariprasad Vijayan comments:

Just added <p>Twitter:<br />Empty</p> in else part.

You need to add similar in facebook too, like

<p>Facebook:<br />Empty</p>
Ask if you need any help.


Sean Gartlan comments:

Hari,

Yes got it... It works fine...

Thanks again for the awesome assistance...

Thanks,
Sean.

2014-01-04

Sachindra Narayan answers:

if you wish to have an external link there, why dont you use an absolute url..

<p>Connect On Facebook:<br /><a class="facebook" href="https://www.facebook.com/">Facebook Wall</a>...


for custom thing...just add "http://" there in front of the url... i guess that would work...