Skip to content

Instantly share code, notes, and snippets.

@qutek
Created June 4, 2015 04:58
Show Gist options
  • Save qutek/bc785c64f7042217d7dd to your computer and use it in GitHub Desktop.
Save qutek/bc785c64f7042217d7dd to your computer and use it in GitHub Desktop.
[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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment