Return The First Link in a Post

Get the URL in the post meta (if it exists) or the first link found in the post content. Falls back to the post permalink if no URL is found in the post. Perfect for the “link” post format.

Usage: Put the function in functions.php, and use it inside the loop in your template files.

Code Snippet:

function get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}

Posted in Pure hacks, Posts