Auto Post Thumbnail - Version 1.0

Version Description

  • First release
Download this release

Release Info

Developer adityamooley
Plugin Icon 128x128 Auto Post Thumbnail
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (2) hide show
  1. auto-post-thumbnail.php +153 -0
  2. readme.txt +26 -0
auto-post-thumbnail.php ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /*
4
+ Plugin Name: Auto Post Thumbnail
5
+ Plugin URI: http://www.sanisoft.com/blog/2010/04/19/wordpress-plugin-automatic-post-thumbnail/
6
+ Description: Automatically generate the Post Thumbnail from the first image in post only if Post Thumbnail is not set manually.
7
+ Version: 1.0
8
+ Author: Aditya Mooley <adityamooley@sanisoft.com>
9
+ Author URI: http://www.sanisoft.com/blog/author/adityamooley/
10
+ */
11
+
12
+ /* Copyright 2009 Aditya Mooley (email : adityamooley@sanisoft.com)
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License as published by
16
+ the Free Software Foundation; either version 2 of the License, or
17
+ (at your option) any later version.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program; if not, write to the Free Software
26
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
+ */
28
+
29
+
30
+ add_action('publish_post', 'apt_publish_post');
31
+ add_action( 'admin_notices', 'apt_check_perms');
32
+
33
+ /**
34
+ * Check whether the required directory structure is available so that the plugin can create thumbnails if needed.
35
+ * If not, don't allow plugin activation.
36
+ */
37
+ function apt_check_perms() {
38
+ $uploads = wp_upload_dir(current_time('mysql'));
39
+
40
+ if ($uploads['error']) {
41
+ echo '<div class="updated"><p>';
42
+ echo $uploads['error'];
43
+
44
+ if ( function_exists('deactivate_plugins') ) {
45
+ deactivate_plugins('auto-post-thumbnail/auto-post-thumbnail.php', 'auto-post-thumbnail.php' );
46
+ echo '<br /> This plugin has been automatically deactivated.';
47
+ }
48
+
49
+ echo '</p></div>';
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Function to save first image in post as post thumbmail.
55
+ */
56
+ function apt_publish_post($post_id)
57
+ {
58
+ global $wpdb;
59
+
60
+ // First check whether Post Thumbnail is already set for this post.
61
+ if (get_post_meta($post_id, '_thumbnail_id', true) || get_post_meta($post_id, 'skip_post_thumb', true)) {
62
+ return;
63
+ }
64
+
65
+ $post = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE id = $post_id");
66
+
67
+ // Initialize variable used to store list of matched images as per provided regular expression
68
+ $matches = array();
69
+
70
+ // Get all images from post's body
71
+ preg_match_all('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $post[0]->post_content, $matches);
72
+
73
+ if (count($matches)) {
74
+ foreach ($matches[0] as $key => $image) {
75
+ /**
76
+ * If the image is from wordpress's own media gallery, then it appends the thumbmail id to a css class.
77
+ * Look for this id in the IMG tag.
78
+ */
79
+ preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
80
+ $thumb_id = $thumb_id[1];
81
+
82
+ // Ok. No id found. Some other way used to insert the image in post. Fetch the image from URL and do the needful.
83
+ if (!$thumb_id) {
84
+ $thumb_id = apt_generate_post_thumb($matches, $key);
85
+ }
86
+
87
+ // If we succeed in generating thumg, let's update post meta
88
+ if ($thumb_id) {
89
+ update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ }// end apt_publish_post()
95
+
96
+ /**
97
+ * Function to fetch the image from URL and generate the required thumbnails
98
+ */
99
+ function apt_generate_post_thumb($matches, $key)
100
+ {
101
+ $imageUrl = $matches[1][$key];
102
+
103
+ // Get the file name
104
+ $filename = substr($imageUrl, (strrpos($imageUrl, '/'))+1);
105
+
106
+ if (!(($uploads = wp_upload_dir(current_time('mysql')) ) && false === $uploads['error'])) {
107
+ return null;
108
+ }
109
+
110
+ // Generate unique file name
111
+ $filename = wp_unique_filename( $uploads['path'], $filename );
112
+
113
+ // Move the file to the uploads dir
114
+ $new_file = $uploads['path'] . "/$filename";
115
+
116
+ file_put_contents($new_file, @file_get_contents($imageUrl));
117
+
118
+ // Set correct file permissions
119
+ $stat = stat( dirname( $new_file ));
120
+ $perms = $stat['mode'] & 0000666;
121
+ @ chmod( $new_file, $perms );
122
+
123
+ // Get the file type. Must to use it as a post thumbnail.
124
+ $wp_filetype = wp_check_filetype( $filename, $mimes );
125
+
126
+ extract( $wp_filetype );
127
+
128
+ // No file type! No point to proceed further
129
+ if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) ) {
130
+ return null;
131
+ }
132
+
133
+ // Compute the URL
134
+ $url = $uploads['url'] . "/$filename";
135
+
136
+ // Construct the attachment array
137
+ $attachment = array(
138
+ 'post_mime_type' => $type,
139
+ 'guid' => $url,
140
+ 'post_parent' => null,
141
+ 'post_title' => '',
142
+ 'post_content' => '',
143
+ );
144
+
145
+ $thumb_id = wp_insert_attachment($attachment, $file, $post_id);
146
+ if ( !is_wp_error($thumb_id) ) {
147
+ wp_update_attachment_metadata( $thumb_id, wp_generate_attachment_metadata( $thumb_id, $new_file ) );
148
+
149
+ return $thumb_id;
150
+ }
151
+
152
+ return null;
153
+ }
readme.txt ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Auto Post Thumbnail ===
2
+ Contributors: adityamooley
3
+ Tags: post thumbnail
4
+ Requires at least: 2.9.1
5
+ Tested up to: 2.9.2
6
+ Stable tag: 1.0
7
+
8
+ Automatically generate the Post Thumbnail from the first image in post only if Post Thumbnail is not set manually.
9
+
10
+ == Description ==
11
+
12
+ Auto post thumbnail is a plugin to generate post thumbnail from first image in post. If the first image doesn't work it will automatically search for the next one and so on until the post thumbnail is inserted.
13
+
14
+ If the post thumbnail is already present, the plugin will do nothing.
15
+ If you don't want a post thumbnail for some post with images, just add a custom field *skip_post_thumb* to the post and the plugin will restrain itself from generating post thumbnail.
16
+
17
+ == Installation ==
18
+
19
+ 1. Upload directory 'auto-post-thumbnail' to the '/wp-content/plugins/' directory
20
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
21
+ 3. Sorry, no more steps :)
22
+
23
+ == Changelog ==
24
+
25
+ = 1.0 =
26
+ * First release