How to automatically set the first image from a post as a thumbnail? Or link the post title to an external resource? Ready-made function.
EN

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

5.00 /5 - (22 votes )
Last verified: May 1, 2026
2min 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-ready GEO-ready AEO-ready 1 Q&A
How do you implement How to extract the first link from post content (PHP snippet)?
Start with a baseline audit, define scope and constraints, then roll out improvements in small, testable steps.

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

Let’s discuss

Related Articles

Need to show the category hierarchy in your theme? Learn how to get the current category and its parent using get_the_category() and cat_is_ancestor_of().
development

How to display current and parent category of a post (WordPress snippet)

Need to show the category hierarchy in your theme? Learn how to get the current category and its parent using get_the_category() and cat_is_ancestor_of().

A practical WordPress snippet for adding parent and ancestor page slugs to body_class(), so you can target site sections with cleaner CSS.
wordpress

Add Parent Page Slugs to WordPress Body Classes

A practical WordPress snippet for adding parent and ancestor page slugs to body_class(), so you can target site sections with cleaner CSS.

How to start as a WordPress developer in 2026. Local environment, theme and plugin development, REST API and headless paths, security and Core Web Vitals. A practitioner playbook that does not waste your first month.
wordpress

WordPress development tutorial: a comprehensive guide for beginners in 2026

How to start as a WordPress developer in 2026. Local environment, theme and plugin development, REST API and headless paths, security and Core Web Vitals. A practitioner playbook that does not waste your first month.