lafif-a
6/4/2015 - 4:58 AM

[Wordpress][Snipets] Get featured image url or first image on content

[Wordpress][Snipets] Get featured image url or first image on content

<?php
/**
 * Get the Featured image URL of a post
 * @global object $post
 * @param string $size
 * @return string
 */
function funkmo_get_post_thumbnail_url($post_id='') {
	$image_url = '';
	
	$thumb = get_post_thumbnail_id($post_id);
  //all good. we have a featured image
  $featured_image_url = wp_get_attachment_url( $thumb );
  if ( $featured_image_url ) {
    $image_url = $featured_image_url;
  } else {
		global $post;
		ob_start();
		ob_end_clean();
		$output = preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post->post_content, $matches);
		$image_url = isset( $matches[1][0] ) ? $matches[1][0] : null;
	}
	
	//Defines a default image
	// if ( empty( $image_url ) ) {
	// 	get default image from options
	// }
	
	return $image_url;
}