WordPress tip: How to get the first link in post

Put the function below in your functions.php file, then use it inside the loop in your template files.

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() );
}

Thanks to Filip Stefansson for the tip!

By the way, I just created a new service called WPCAT, where I offer my help to fix or enhance your WordPress site for a cheap rate. Don’t wait and contact me right now if you need any help with WordPress!

Leave a Reply

Your email address will not be published. Required fields are marked *