Make URL in custom field hyper link

In my WordPress site I have created a custom field for author website link. but I don’t know how to make it hyperlink, so people can click on it to surf that website. at this moment it only shows raw text. like: "www.example.com"

my code is:

<?php echo get_post_meta($post->ID, 'Author Website', true); ?>

2 Answers
2

Use this.

get_post_meta($post->ID, 'custom-field-name', true);

Where custom-field-name is the name of the custom field. You will have to add your custom field name in place of custom-field-name.

Also to open link in new browser window/tab add target="_blank". Always use esc_url() for URLs and esc_html() when you want to display a value without working HTML.

$value = get_post_meta( $post->ID, 'Author Website', true );

if ( $value ) {
    // Returns an empty string for invalid URLs
    $url = esc_url( 'http://' . $value );

    if ( '' !== $url ) {
        $display = esc_html( $value );

        print "<a href="https://wordpress.stackexchange.com/questions/159392/$url" target="_blank">$display</a>";
    }
}

You should also store the protocol in the field, because some websites might not be available per http, just per https.

Leave a Comment