PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Post field to collect a web address as clickable URL

  1. draney
    Member

    I'm using a form to create a draft post and need to collect a web address that will be a clickable URL. I have tried using a custom field and then my template pulls the the URL in to create a link in the body of the post with this code:

    <?php  if((get_post_meta($post->ID, 'website_link', true))) { ?>
       <p><a href="<?php echo get_post_meta($post->ID, 'website_link', true); ?>">Click to view website &raquo;</a></p>
    <?php } ?>

    But I get too many invalid URLs because some people leave off the http://. So I want to try using an advanced website field to collect the URL entry, but I don't want it to show a text URL in the post. If they enter a URL, I want it to be appended to the bottom of the post as a clickable link similar the way I am doing it now, with the actual URL hidden and just showing a generic "Click Here" type of link.

    How can I do this?

    Posted 11 years ago on Thursday May 2, 2013 | Permalink
  2. I modified your script to check the custom field before posting the URL. Now it looks to see if the URL begins with "http://" before posting it and if not it adds it to the URL.

    <?php  if((get_post_meta($post->ID, 'website_link', true))) {
    $websiteLink = get_post_meta($post->ID, 'website_link', true);
    if(substr($websiteLink,0,7) == 'http://'){ ?>
    <p><a href="<?php echo get_post_meta($post->ID, 'website_link', true); ?>">Click to view website &raquo;</a></p>
    <?php } else { ?>
    <p><a href="http://<?php echo get_post_meta($post->ID, 'website_link', true); ?>">Click to view website &raquo;</a></p>
    <?php } } ?>

    Hope this does the job.

    Posted 11 years ago on Thursday May 2, 2013 | Permalink
  3. David Peralty

    Very clean pierski :)

    Posted 11 years ago on Thursday May 2, 2013 | Permalink
  4. draney
    Member

    Cool and Thanks pierski!!

    Posted 11 years ago on Thursday May 2, 2013 | Permalink

This topic has been resolved and has been closed to new replies.