• Resolved Imenicaa

    (@imenicaa)


    Hello folks!

    I created custom field named “productlink”, and i am showing it on my homepage using this piece of code, and it works great:

    <?php $field_name="productlink"; $field_value = get_post_meta($post->ID, $field_name, true); ?>
    <a href="<?php echo $field_value; ?>" class="readmore-button">BUY IT NOW</a>

    This piece of code shows the “Buy it now” link which works perfectly if the user enters link with http:// in the custom field.
    If he just enters (for example) http://www.google.com, link on the homepage becomes http://www.mywebsite.com/www.google.com.

    I am not native english speaker, so i hope i explained it well.
    Any help would be great!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    //Add in "http://" if it isn't there already
    if ( strpos( $link, 'http://' ) === false ) {
        $link = 'http://' . $link;
    }
    Thread Starter Imenicaa

    (@imenicaa)

    Hey Andrew!Thank you for your help!

    I am PHP noob, it took me way long to code even this part, can you explain me how to add that and where…
    I know im asking too much, but hey, maybe you are in the mood to help 🙂

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Could you re-post your code but this time wrap it in backticks so that it doesn’t get corrupted?

    Thread Starter Imenicaa

    (@imenicaa)

    I think i edited it above, but just in case, here it is again:

    <?php $field_name="productlink"; $field_value = get_post_meta($post->ID, $field_name, true); ?>
    <a href="<?php echo $field_value; ?>" class="readmore-button">BUY IT NOW</a>

    Thank you so much!

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Try this instead:

    <?php
     $field_name="productlink";
     $field_value = get_post_meta($post->ID, $field_name, true);
     //Add in "http://" if it isn't there already
     if ( strpos( $field_value, 'http://' ) === false ) {
        $field_value = 'http://' . $field_value;
     }
    ?>
    <a href="<?php echo $field_value; ?>" class="readmore-button">BUY IT NOW</a>

    Thread Starter Imenicaa

    (@imenicaa)

    Yep, it works perfectly!! Thanks a bunch Andrew!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom field value to URL’ is closed to new replies.