Find first image URL in WordPress post content

Find first image URL in WordPress post content

Created:29 Dec 2016 06:59:28 , in  Web development

With ever growing importance of websites like Pinterest, and in particular due to their reliance on images, and for general post linking purposes often it is desirable or even necessary to be able to find at least one image in your WordPress blog entry automatically and in an efficient manner.

Most of the time, in fact in 99% of cases ,what you need is not the whole img HTML tag but it's src attribute value, or in other words its Universal Resource Locator (URL). In this entry I'll present a concise technique with which you can find find desired URL.

The code

Below is a PHP function, which scans WordPress post contents for img HTML tags, and if it finds at least one, returns its src attribute.


function find_first_image_src($post) {
    $matches = array();
	preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	return count($matches[1]) > 0 ? $matches[1][0] : NULL;
}

Usage

In a case when no image is found, NULL will be returned. You might need to use isset() PHP function if you have to do something more with the src value.


if(isset(find_first_image_src($post))){
    //do stuff here ...
}else{
    // no image found in the post contents
}

$post variable is a WordPress $post object. It is available through global variable $post.


global $post;
$first_img_url = find_first_image_src($post);

or if you need a post with a specific ID instead of the current post, you can employ WordPress function get_post() as follows:


$post = get_post(get_the_ID());
if( isset($post) ){
  $first_img_url = find_first_image_src($post);
  // do stuff with first_img_url 
}  

Here is more about get_post()

The chunk of code above can go either in your theme function.php file or in a plugin.

More than one image URL

The above technique can be easily adapted to find not only the first image URL but any post image URL. Just replace 0 in $matches[1][0] with whatever number you need (since $matches[1] is a PHP array, matches are 0 indexed), if you need all the post image URLs, just grab $matches[1].

Parting thought

Wrapping up, the above described technique can be adapted to find other tags attribute values, but I think that's a subject for another post. For not I hope you'll find information in this entry useful and adaptable to your code.

This post was updated on 25 May 2017 16:10:05

Tags:  image ,  php ,  wordpress 


Author, Copyright and citation

Author

Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, is sWWW admin and owner.He enjoys doing Maths and studying algorithms, writing code in scripting and command languages, Thrash Metal music and playing electric guitar.

Copyrights

©Copyright, 2017 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.

Citation

Cite this article as:

Wojnowski, Sylwester. "Find first image URL in WordPress post content." From sWWW - Code For The Web . https://wojnowski.net.pl//main/index/find-first-image-url-in-wordpress-post-content