WordPress tip: How to get the first link in post

October 7, 2013 at 3:41 pm

Remember when I shown you how to get the first image from a post? Today, I’m going to let you know how you can easily grab the first link from the post content.

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!