How to extract the first link from post content (PHP snippet)
EN

How to extract the first link from post content (PHP snippet)

Last verified: June 1, 2026
2 min read
Tutorial
Full-stack developer

Sometimes we build “News Aggregator” type themes, where a post doesn’t have its own content, but only links to an external article. Or we want the first image in the content to automatically become the “Featured Image” if the editor forgets to set it.

Learn more about professional WordPress development at WPPoland. In both cases, we need to “scan” the post content (the_content) and extract the first <a> or <img> tag from it.

#Method: Domdocument class

Many developers use Regular Expressions (Regex) for this, but parsing HTML with Regex is bad practice. It’s better to use PHP’s built-in DOMDocument class.

Here’s a ready-made function you can paste into functions.php:

function get_first_link_url( $content ) {
    // If content is empty, return false
    if ( empty( $content ) ) return false;

    $doc = new DOMDocument();
    
    // Suppress HTML5 errors (DOMDocument is old and sometimes complains about <section> etc.)
    libxml_use_internal_errors(true);
    
    // Load HTML (with UTF-8 hack)
    $doc->loadHTML( mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8') );
    
    $links = $doc->getElementsByTagName('a');

    if ( $links->length > 0 ) {
        // Return href of the first link
        return $links->item(0)->getAttribute('href');
    }

    return false;
}

#Usage IN the loop

$link = get_first_link_url( get_the_content() );

if ( $link ) {
   echo '<a href="' . esc_url($link) . '" class="read-more-external">Read original</a>';
}

This solution is solid, secure, and handles errors in HTML structure better than any Regex.

Next step

Turn the article into an actual implementation

This block strengthens internal linking and gives readers the most relevant next move instead of leaving them at a dead end.

Want this implemented on your site?

If you want to convert the article into a working site improvement, redesign, or build plan, I can define the scope and implement it.

Related cluster

Explore other WordPress services and knowledge base

Strengthen your business with professional technical support in key areas of the WordPress ecosystem.

Article FAQ

Frequently Asked Questions

Practical answers to apply the topic in real execution.

SEO-readyGEO-readyAEO-ready1 Q&A
How do you implement How to extract the first link from post content (PHP snippet)?#
Define the current state, constraints and success criteria, then make one measurable change at a time.

Need an FAQ tailored to your industry and market? We can build one aligned with your business goals.

Let’s discuss

Related Articles