-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-first-image-as-featured-image.php
125 lines (103 loc) · 3.67 KB
/
set-first-image-as-featured-image.php
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
<?php
add_action('wp_footer', function () {
if (is_single()) {
global $post;
set_to_true_post_thumbnail($post);
}
});
function set_post_thumbnail_this_post($post_id, $post = null)
{
if (empty($post)) {
$post = get_post($post_id);
}
$upload_dir = wp_upload_dir();
$url = get_first_image_as_featured($post->post_content, $post->post_name, $post_id);
$basename = basename($url);
if ( strpos($basename, '?') !== false ) {
$ext_arr = explode('?', $basename);
$basename = $ext_arr[0];
}
$ext = phpinfo($basename, PATHINFO_EXTENSION );
if ( empty($ext) ) {
$ext = 'jpg';
}
if (!empty($url)) {
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$filename = $upload_dir['path'] . '/' . $post->post_name . '.' . $ext;
$content = file_get_contents($url);
if ( $content === false ) {
$content = file_get_contents('http:'. $url);
}
if ( $content === false ) {
$content = file_get_contents('https:'. $url);
}
if ($content !== false) {
file_put_contents($filename, $content);
$wp_filetype = wp_check_filetype(basename($filename), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $post->post_title,
'post_content' => '',
'post_status' => 'inherit',
'guid' => $upload_dir['url'] . '/' . $post->post_name . '.' . $ext
);
$attach_id = wp_insert_attachment($attachment, $filename);
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
wp_update_image_subsizes($attach_id);
$set_thumbnail = set_post_thumbnail($post_id, $attach_id);
if ($set_thumbnail != false) {
update_post_meta($post_id, '__has_replace_thumbnail', $attach_id);
}
}
}
}
function get_first_image_as_featured($content, $slug = 'tes-slug', $post_id = null)
{
preg_match_all('/<img[^>]+>/i', $content, $result);
$img = array();
foreach ($result[0] as $img_tag) {
preg_match_all('/(src)=("[^"]*")/i', $img_tag, $img[$img_tag]);
}
$image_url = '';
foreach ($img as $value) {
if (!empty($value[2][0])) {
$image_url = $value[2][0];
$image_url = str_replace('"', '', $image_url);
if (!empty(getimagesize($image_url))) {
break;
}
if (!empty(getimagesize('http:' . $image_url))) {
break;
}
if (!empty(getimagesize('https:' . $image_url))) {
break;
}
}
}
return $image_url;
}
function set_to_true_post_thumbnail($post)
{
$featured_image_exists = has_post_thumbnail($post->ID);
if ( !$featured_image_exists ) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ( $attached_image ) {
foreach ( $attached_image as $attachment_id => $attachment ) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
} else {
$__has_replace_thumbnail = get_post_meta($post->ID, '__has_replace_thumbnail', true);
$thumbnail_url = get_the_post_thumbnail_url($post);
if (empty($__has_replace_thumbnail) || empty($thumbnail_url) ) {
if (has_post_thumbnail($post) && $thumbnail_url && strpos($thumbnail_url, home_url()) === false) {
update_post_meta($post->ID, '__has_replace_thumbnail', get_post_thumbnail_id($post));
} else {
set_post_thumbnail_this_post($post->ID, $post);
}
}
}
}