WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the code I'm using to add a custom field to a post type I have set up. As you can see it creates an input box which allows me to add whatever I want. I want to put urls in this box (without the "http://", e.g. "example.com") and have them be automatically clickable. Is there a way?

add_action("admin_init", "add_friends_fields");

function add_friends_fields(){
    add_meta_box("artist_links", "Links", "artist_links", "friends", "normal", "low");

function artist_links(){
    global $post;
    $custom = get_post_custom($post->ID);
    $artist_links = $custom["artist_links"][0];
    <label>Links:</label><br /><br />
    <input size="50" name="artist_links" value="<?php echo $artist_links; ?>" />


    <section id="artists">
        <h2 class="title"><span>Artists</span></h2>

            $temp = $wp_query;
            $wp_query = null;
            $wp_query = new WP_Query();

            while ( $wp_query->have_posts() ) : $wp_query->the_post();

            $custom = get_post_custom($post->ID);
            $artist_links = $custom["artist_links"][0];
            $artist_bio = $custom["artist_bio"][0];

        <article class="mgm-artist<?php if($i%6 == 0) { echo ' right'; }; $i++; ?>" id="post-<?php the_ID(); ?>">
            <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'video-thumb' ); } ?>
            <section class="artist-details">
                <p><span>Name:</span> <?php the_title(); ?></p>
                <p><span>Bio:</span> <?=$artist_bio?></p>
                <p><span>Link(s):</span> <?=$artist_links?></p>

            $wp_query = null;
            $wp_query = $temp;
share|improve this question
possible duplicate of Turning static urls into live links – t31os Feb 23 '11 at 20:32
Yes, it is. Sorry for posting twice. I tried to explain my needs better in this question since my last question wasn't clear judging by the answer I got. I can update the other one if needed. – J82 Feb 23 '11 at 20:40
@J82: Can you please present the code for where you are printing the custom field value to the page? That would be the more relevant part. – wyrfel Feb 23 '11 at 21:33
@wyrfel I may be wrong but isn't that in the code I posted? I checked my functions.php and the only other code is for registering the post type and saving the fields. – J82 Feb 23 '11 at 21:37
@wyrfel I think I misunderstood what you said. I've added the code that is in the page template. – J82 Feb 23 '11 at 22:50
up vote 0 down vote accepted

put this in your functions.php:

function my_format_artist_links($urls) {
    $urls = explode(',', $urls);
    $links = array();
    foreach ($urls as $url) {
        if (empty($url)) continue;
        $links[] = '<a href="http://'.$url.'">'.$url.'</a>';
    return implode(',', $links);

and then replace

<p><span>Link(s):</span> <?=$artist_links?></p>


<p><span>Link(s):</span> <?php echo my_format_artists_links($artist_links); ?></p>'
share|improve this answer
Thanks @wyrfel. There was an extra 's' added in the echo command so I changed that. One problem I'm having is getting the http:// to prepend the urls since I'm typing them in like this example.com, example2.com, etc. I tried adding http:// to the function like this: <a href="http://'.$url.'">'.$url.'</a> however that only made the first url link in the correct format (i.e. http://example.com) while the rest of the urls linked to %20example2.com i.e. with %20 in front of it. Also, there is the problem of a comma showing up after the last item which is not needed. – J82 Feb 24 '11 at 7:57
Your approach should work. Are you sure your links are properly comma-separated. This sounds like you are just getting them all stacked together by something else, not a ','. And if you get a comma after the last, it must be at the end of your custom post value. However, i'm making an edit, that get's rid of it. – wyrfel Feb 24 '11 at 8:05
Yes, my links weren't properly comma-separated. I was adding a space after the comma. So to make this work, I added a space after the comma on lines 2 and 8 of your code. I tried putting in the new line of code on line 5 if (empty($url)) continue; but the last comma is still there and won't go away. I'm not sure what you mean by it must be at the end of your custom post value. – J82 Feb 24 '11 at 8:17
implode() only inserts the comma between the elements of the array passed, not at the end. since there's no commas in my code elsewhere, i thought it could be at the end of your custom field value (at the end of your links). However, since the if-empty clause should care for that, it must be elsewhere in your code. – wyrfel Feb 24 '11 at 8:22
Ah, spot on. There was an extra comma in the html. Thanks for your help! – J82 Feb 24 '11 at 8:35

just change $artist_links = $custom["artist_links"][0]; in your loop to

$artist_links = '<a href="'.$custom['artist_links'][0].'">'.get_the_title($post->ID).'</a>;
share|improve this answer
Thank you for helping. I tried what you suggested and the links came up with the title of the post. Also, if I just enter "example.com" without the "http://", the links turn into "site.com/example.com". One last thing is if there are multiple urls separated by commas such as "example.com, example2.com, example3.com", is there a way to turn them all into their own clickable links instead of having them combined? Right now, if I put in multiple urls, it comes out like this: "sitename.com/example.comexample2.comexample3.com" – J82 Feb 24 '11 at 4:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.