Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mzdebo/f4293ab2e0b2b62c6ecb6240c4aa11ec to your computer and use it in GitHub Desktop.
Save mzdebo/f4293ab2e0b2b62c6ecb6240c4aa11ec to your computer and use it in GitHub Desktop.
WP function auto featured image
<?php
/*
* This function will get the first image in this order:
*
* 1) Featured image
* 2) First image attached to post
* 3) First image URL in the content of the post
* 4) YouTube screenshot
* 5) Default images for different categories [SET MANUALLY]
* 6) Backup-default image if all else fails [SET MANUALLY]
*
* A big THANKS goes out to Michelle @ WordPress.StackExchange.com
* for her answer (http://bit.ly/O3UaLm) that made this possible!!!
*
*/
//
// Activate post-thumbnails by adding this to theme's function.php file
//
add_theme_support('post-thumbnails');
//
// Add the following to the theme's function.php file
//
function vp_get_thumb_url($text, $size){
global $post;
$imageurl="";
// 1) FEATURED IMAGE
// Check to see which image is set as "Featured Image"
$featuredimg = get_post_thumbnail_id($post->ID);
// Get source for featured image
$img_src = wp_get_attachment_image_src($featuredimg, $size);
// Set $imageurl to Featured Image
$imageurl=$img_src[0];
// 2) 1ST ATTACHED IMAGE IMAGE
// If there is no "Featured Image" set, move on and get the first image attached to the post
if (!$imageurl) {
// Extract the thumbnail from the first attached imaged
$allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
foreach ($allimages as $img){
$img_src = wp_get_attachment_image_src($img->ID, $size);
break;
}
// Set $imageurl to first attached image
$imageurl=$img_src[0];
}
// 3) 1ST IMAGE URL IN POST
// If there is no image attached to the post, look for anything that looks like an image and get that
if (!$imageurl) {
preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' , $text, $matches);
$imageurl=$matches[1];
}
// 4) YOUTUBE SCREENSHOT
// If there's no image attached or inserted in the post, look for a YouTube video
if (!$imageurl){
// look for traditional youtube.com url from address bar
preg_match("/([a-zA-Z0-9\-\_]+\.|)youtube\.com\/watch(\?v\=|\/v\/)([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
$youtubeurl = $matches2[0];
$videokey = $matches2[3];
if (!$youtubeurl) {
// look for youtu.be 'embed' url
preg_match("/([a-zA-Z0-9\-\_]+\.|)youtu\.be\/([a-zA-Z0-9\-\_]{11})([^<\s]*)/", $text, $matches2);
$youtubeurl = $matches2[0];
$videokey = $matches2[2];
}
if ($youtubeurl)
// Get the thumbnail YouTube automatically generates
// '0' is the biggest version, use 1 2 or 3 for smaller versions
$imageurl = "http://i.ytimg.com/vi/{$videokey}/0.jpg";
}
// 4) YOUTUBE SCREENSHOT
// If there's no YouTube video in the post, look for the default based on the category
if (!$imageurl) {
// Set default Image for different categories
// [SET DIRECTORY MANUALLY!!]
$dir = get_template_directory_uri() . '/images/'; // [SET MANUALLY!!!]
// [DID YOU SET YOUR DIRECTORY?!]
$get_cat = get_the_category();
$cat = $get_cat[0]->
slug;
// [SET IMG EXT MANUALLY!!]
$imageurl = $dir . $cat . '.jpg'; // [SET MANUALLY!!!]
// [DID YOU SET YOUR IMG EXT?!]
// Use this array if you have a few main categories that you want images for
$array = array( 'cat_1', 'cat_2', 'cat_3',);
if (!in_array($cat, $array))
// [SET BACKUP IMAGE MANUALLY!!!]
$imageurl = $dir . 'district.jpg'; // [SET MANUALLY!!!]
// [DID YOU SET YOUR BACKUP IMAGE?!]
}
// Spit out the image path
return $imageurl;
}
//
// Add inside loop but before displaying image
//
if (function_exists('vp_get_thumb_url')) {
// Set the desired image size.
// Swap out 'thumbnail' for 'medium', 'large', or '[your-custom-size]'
$thumb=vp_get_thumb_url($post->post_content, 'large-feature');
}
//
// Call the image inside the loop
//
?>
<a href="<?php get_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="featured-image">
<img src="<?php
// Gets the image URL
if ($thumb!='') echo $thumb;
?>" class="attachment-thumbnail wp-post-image" />
</a>
<?php
/*
* A big THANKS goes out to Michelle @ WordPress.StackExchange.com
* for her answer (http://bit.ly/O3UaLm) that made this possible!!!
*
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment