-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpost-thumbnail-fallback.php
More file actions
139 lines (127 loc) · 5.24 KB
/
post-thumbnail-fallback.php
File metadata and controls
139 lines (127 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
Plugin Name: Post Thumbnail Fallback
Plugin URI: https://github.com/BostonWP/functionality-plugins
Description: Generates HTML for a posts thumbnail using multiple fallbacks to find an image.
Version: .1
Author: Jon Bishop
Author URI: http://www.jonbishop.com
License: GPLv2 or later
*/
function bwp_get_image_html($post_thumbnail_url, $size = 'thumbnail', $attr = '') {
$src = $post_thumbnail_url;
if (is_array($size)) {
$width = $size[0];
$height = $size[1];
}
$hwstring = image_hwstring($width, $height);
if (is_array($size))
$size = join('x', $size);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size"
);
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters('wp_get_attachment_image_attributes', $attr);
$attr = array_map('esc_attr', $attr);
$html = rtrim("<img $hwstring");
foreach ($attr as $name => $value) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
return $html;
}
/**
* Returns image HTML for a post with multiple fallbacks
*
* This function generates HTML for a posts thumbnail using multiple methods to
* find an image.
*
* First the function checks to see if there are any images attached the post.
* If there is an image, we can use built core functions to generate the html.
*
* Next the function checks to see if there are any images in the post's content.
* If an image is found, the function resizes the image using the images.weserv.nl
* web service.
*
* Finally the function uses flickholdr.com to generate a relavent image using the
* specifed dimensions.
*
* Filters:
* bwp_get_image_default - Define an alternative default image to flickholdr.com
* bwp_get_image_html - Filter HTML generated by bwp_get_image()
*
* @param string $html Image HTML.
* @param int $post_id The current post's ID
* @param string|array $size Size of what the result image should be.
* @return string Image HTML.
*/
function bwp_get_image($html = "", $post_id = null, $size = 'featured-image', $attr) {
global $post, $_wp_additional_image_sizes;
if ($html == "") {
// First let's check for any attachments
$attachments = get_children(array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ASC'
));
if (!empty($attachments)) {
foreach ($attachments as $attachment_id => $attachment) {
if (wp_get_attachment_image($attachment_id) != ""):
$post_thumbnail_url = wp_get_attachment_url($attachment_id);
$html = wp_get_attachment_image($attachment_id, $size, false, $attr);
endif;
}
} else {
// Create image dimensions for our custom image
if (is_array($size)) {
$width = $size[0];
$height = $size[1];
} else {
if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes) && in_array($size, array_keys($_wp_additional_image_sizes))) {
$width = intval($_wp_additional_image_sizes[$size]['width']);
$height = intval($_wp_additional_image_sizes[$size]['height']);
}
}
// Check for any images in the posts
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
if (!empty($matches[1][0])) {
$post_thumbnail_url = $matches[1][0];
$post_thumbnail_url = 'http://images.weserv.nl/?url=' . $post_thumbnail_url . '&h=' . $width . '&h=' . $height . '&t=fit';
// Fall back image URL
} else {
$f_tags = array();
$query_tags = "";
// Check for tags or categories to grab relevant images
$categories = get_the_category();
foreach ($categories as $category) {
$f_tags[] = $category->slug;
break;
}
$tags = get_the_tags();
foreach ($tags as $tag) {
$f_tags[] = $tag->slug;
break;
}
$f_tags = array_diff($f_tags, array('uncategorized'));
if(!empty($f_tags)){
$query_tags = implode(",", $f_tags);
}
$post_thumbnail_url = "http://flickholdr.com/" . $width . "/" . $height . "/" . $query_tags;
$post_thumbnail_url = apply_filters('bwp_get_image_default', $post_thumbnail_url);
}
// Create image html with our custom image urls
$html = apply_filters('bwp_get_image_html', bwp_get_image_html($post_thumbnail_url, array($width, $height), $attr));
}
}
return $html;
}
function bwp_filter_post_thumbnail($html, $post_id, $post_thumbnail_id, $size, $attr) {
return apply_filters('bwp_filter_post_thumbnail', bwp_get_image($html, $post_id, $size, $attr));
}
add_filter('post_thumbnail_html', 'bwp_filter_post_thumbnail', 10, 5);
?>