├── .gitignore
├── fibs-plugin.zip
├── README.md
└── fibs-plugin
├── readme.txt
└── featuredimageplugin.php
/.gitignore:
--------------------------------------------------------------------------------
1 | Wordpress SVN Releases/**
--------------------------------------------------------------------------------
/fibs-plugin.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/objects/fibs/master/fibs-plugin.zip
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # FIBS: Featured Image Bulk Set
2 |
3 | This is a plugin designed to do one simple job: programatically add a featured image in WordPress to existing posts.
4 |
5 | I've worked on a couple older WordPress blogs which had hundreds of posts in the old format, where Featured Images wasn't really a 'thing'. Adding a new Featured Image to each one would take days of effort. With this plugin, you can quickly and easily use either the first or last image from within the post to be the new Featured Image.
6 |
7 | ## Blog Describing the Plugin
8 |
9 | https://blog.nickleghorn.com/2021/01/25/adding-a-featured-image-to-all-posts-in-wordpress-in-one-easy-click/
10 |
11 | ## GitHub Repository
12 |
13 | https://github.com/foghorn/fibs
14 |
15 | ## Wordpress Plugin Repo Page
16 |
17 | https://wordpress.org/plugins/featured-image-bulk-set/
18 |
19 | ## Fair Warning: This Code is Terrible, but Effective
20 |
21 | No one here is claiming that this is the best or prettiest way of doing this. But it's here, it works, and it's free.
22 |
23 | ## How to Use
24 |
25 | - Download this code and install it in your wp-plugins folder
26 | - Activete the plugin in your WordPress plugin section of the wp-admin panel
27 | - There should be a new section called "FIBS Menu" added to the Settings section. Click that.
28 | - Select your options and hit "save" to start the process. NOTE: this process cannot be stopped once stared.
29 | - NOTE: If you don't click the box at the bottom, you can do a test run of the process to see what the result will be.
--------------------------------------------------------------------------------
/fibs-plugin/readme.txt:
--------------------------------------------------------------------------------
1 | === Featured Image Bulk Set Plugin ===
2 | Contributors: kc2qcy
3 | Tags: utilities, featured image
4 | Requires at least: 4.7
5 | Tested up to: 6.0
6 | Stable tag: 1.5.4
7 | Requires PHP: 7.0
8 | License: GPLv2 or later
9 | License URI: https://www.gnu.org/licenses/gpl-2.0.html
10 |
11 | This is a plugin designed to do one simple job: programatically add a featured image in WordPress to existing posts. Either images already in the post, or a default image you can select.
12 |
13 | == Description ==
14 |
15 | This plugin adds a section to your Settings menu in the wp-admin section. Through that section you can perform two tasks:
16 |
17 | * A scripted, one-time action updating all of the posts in your blog that do not have a featured image where the script will either try to find a suitable image to use or use one you have provided and configure it for you.
18 | * An ongoing, continuous monitoring for posts that do not have a featured image, using the same logic to try and find a suitable featured image and setting that for the post.
19 |
20 | You can find additional documentation for this plugin on the author's blog [blog.nickleghorn.com](https://blog.nickleghorn.com/2021/01/25/adding-a-featured-image-to-all-posts-in-wordpress-in-one-easy-click/ "blog.nickleghorn.com")
21 |
22 | Special thanks to Robert Farago for giving me yet another massive site without any Featured Images to clean up for the impetus to finally make this available.
23 |
24 | == Changelog ==
25 |
26 | = 1.5.4 =
27 | * Testing for WP 6.0
28 |
29 | = 1.5.2 =
30 | * Fixed an issue where output was not being presented correctly to the end user.
31 |
32 | = 1.5.1 =
33 | * Update readme
34 |
35 | = 1.5 =
36 | * Tested stable release of changes in 1.4.1 and 1.4.2
37 |
38 | = 1.4.2 =
39 | * Fixed a bug that would cause no posts to be returned from the query of the post DB
40 |
41 | = 1.4.1 =
42 | * Added the ability to exclude drafts in posts that get featured images
43 |
44 | = 1.4 =
45 | * Moved post checking items into their own function
46 | * Added configurable options to the script
47 | * Added the ability to automatically have every post update with a featured image, checked every time the page is loaded
48 |
49 | = 1.3 =
50 | * Added unique identifier to function name
51 |
52 | = 1.2 =
53 | * Added input sanitization and output escaping as per the WordPress plugin security standards
54 |
55 | = 1.1 =
56 | * Connected the image override function
57 | * Moved image validation to a seperate function, and validate that image is actually an image before assigning
58 |
59 | = 1.0 =
60 | * First version!
61 |
--------------------------------------------------------------------------------
/fibs-plugin/featuredimageplugin.php:
--------------------------------------------------------------------------------
1 | ";
38 | }
39 | }
40 | else
41 | {
42 | $return = $return . "ERROR: Identified image ID is not an image
";
43 | }
44 | }
45 | else
46 | {
47 | $return = $return . "ERROR: Tried to set the featured image for something that is not a post
";
48 | }
49 | }
50 | else
51 | {
52 | $return = $return . "ERROR: Either the Post ID or the Featured Image ID are not a number
";
53 | }
54 |
55 | return $return;
56 | }
57 |
58 | //Check whether a post has a featured image set, and if none set, find and set a suitable image
59 | function fibs_featured_image_set($Return_ID,$tablename,$con,$dim,$firstlast,$forreal,$override)
60 | {
61 | $return = "";
62 |
63 | //Check that there is a featured image
64 | if (get_post_thumbnail_id($Return_ID) == FALSE)
65 | {
66 | $return = $return . "NO FEATURED IMAGE!
";
67 |
68 | //Find featured image
69 | $img_ref = '';
70 | $img_slice = '';
71 |
72 | //Grab the post content
73 | $E = mysqli_query($con,"SELECT post_content FROM " . $tablename . " WHERE ID = '" . $Return_ID . "'");
74 | $F = mysqli_fetch_array($E);
75 |
76 | //Sanitize content
77 | $return_content = wp_kses_post($F['post_content']);
78 |
79 | if (strlen($return_content) > 0)
80 | {
81 | $return = $return . "Grabbed post: " . wp_kses(md5($return_content),array()) . "
";
82 |
83 | //Check override
84 | if ( (strlen($dim) > 0) AND ($override == 1) )
85 | {
86 | //Check that this is really an image and post
87 | $return = $return . fibs_CheckAndPost($Return_ID,$dim,$forreal);
88 | }
89 | //is there an image to be found?
90 | elseif (substr_count($return_content,'wp-image-'))
91 | {
92 | $return = $return . "Image found!
";
93 |
94 | //First or last image?
95 | if ($firstlast == 0)
96 | {
97 | //Identify the first wp-image- referenced
98 | $img_ref = stripos($return_content,'wp-image-');
99 |
100 | $img_slice = substr($return_content,$img_ref);
101 |
102 | //Find where this string ends
103 | $counter = 9;
104 |
105 | while (preg_match('/^[a-zA-Z0-9\-]$/',substr($img_slice,$counter,1)))
106 | {
107 | $counter++;
108 | }
109 |
110 | //Slice string to just post ID
111 | $thumbnailID = substr($img_slice,9,($counter - 9));
112 |
113 | $return = $return . fibs_CheckAndPost($Return_ID,$thumbnailID,$forreal);
114 |
115 | }
116 | else
117 | {
118 | //Identify the last wp-image- referenced
119 | $img_ref = strripos($return_content,'wp-image-');
120 |
121 | $img_slice = substr($return_content,$img_ref);
122 |
123 | //Find where this string ends
124 | $counter = 9;
125 |
126 | while (preg_match('/^[a-zA-Z0-9\-]$/',substr($img_slice,$counter,1)))
127 | {
128 | $counter++;
129 | }
130 |
131 | //Slice string to just post ID
132 | $thumbnailID = substr($img_slice,9,($counter - 9));
133 |
134 | $return = $return . fibs_CheckAndPost($Return_ID,$thumbnailID,$forreal);
135 | }
136 |
137 | }
138 | elseif (strlen($dim) > 0)
139 | {
140 | //Check that this is really an image and post
141 | $return = $return . fibs_CheckAndPost($Return_ID,$dim,$forreal);
142 |
143 | }
144 | else
145 | {
146 | $return = $return . "ERROR: No image found
";
147 | }
148 |
149 | }
150 | else
151 | {
152 | $return = $return . "ERROR: Zero length post
";
153 | }
154 |
155 | }
156 | else
157 | {
158 | $return = $return . "FEATURED IMAGE SET!
";
159 | }
160 |
161 | return $return;
162 | }
163 |
164 | //Automated check if featured image is set for each post
165 | function fibs_auto_featured_image()
166 | {
167 | //Make sure we actually want it to run
168 | if (get_option('fibs_automated') == 1)
169 | {
170 | //Check that we are on a post that is published
171 | if ((get_post_type() == 'post') AND is_singular() AND ('publish' === get_post_status()))
172 | {
173 |
174 | //check whether there is a featured image
175 | if (get_post_thumbnail_id(get_the_ID()) == FALSE)
176 | {
177 | //Get and sanitize table name
178 | global $wpdb;
179 | $prefix = $wpdb->prefix;
180 | $tablename = sanitize_text_field($prefix . "posts");
181 |
182 | $con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
183 |
184 | //Grab options from the database
185 | $dim = sanitize_text_field(get_option('fibs_dim'));
186 |
187 | if ($dim != '')
188 | {
189 | if (wp_attachment_is_image($dim))
190 | {
191 | $dim = $dim;
192 | }
193 | else
194 | $dim = '';
195 | }
196 | else
197 | $dim = '';
198 |
199 | $override = sanitize_text_field(get_option('fibs_override'));
200 |
201 | if ($override != 1)
202 | $override = 0;
203 |
204 | $forreal = 1;
205 |
206 | fibs_featured_image_set(get_the_ID(),$tablename,$con,$dim,$firstlast,$forreal,$override);
207 | }
208 | }
209 | }
210 | }
211 | add_action( 'wp', 'fibs_auto_featured_image' );
212 |
213 | function fibs_checked_checker($optionkey,$value)
214 | {
215 | if (get_option($optionkey) == FALSE)
216 | {
217 | if ($value == 0)
218 | return "checked=\"checked\"";
219 | }
220 | else
221 | {
222 | $check = sanitize_text_field(get_option($optionkey));
223 |
224 | if ($check == $value)
225 | return "checked=\"checked\"";
226 | else
227 | return "";
228 | }
229 |
230 | }
231 |
232 |
233 | //Add settings page to the Admin menu
234 | function fibs_add_settings_page() {
235 | add_options_page( 'Featured Image Bulk Set', 'FIBS Menu', 'manage_options', 'fibs_plugin', 'fibs_render_plugin_settings_page' );
236 | }
237 | add_action( 'admin_menu', 'fibs_add_settings_page' );
238 |
239 | function fibs_render_plugin_settings_page() {
240 | //Sanitize user input
241 | $execute = sanitize_text_field($_GET['execute']);
242 | $secretcheck = sanitize_text_field($_POST['secretcheck']);
243 | $dim = sanitize_text_field($_POST['dim']);
244 | $override = sanitize_text_field($_POST['override']);
245 | $forreal = sanitize_text_field($_POST['forreal']);
246 | $firstlast = sanitize_text_field($_POST['firstlast']);
247 | $automated = sanitize_text_field($_POST['automated']);
248 | $drafts = sanitize_text_field($_POST['drafts']);
249 |
250 | //Get and sanitize table name
251 | global $wpdb;
252 | $prefix = $wpdb->prefix;
253 | $tablename = sanitize_text_field($prefix . "posts");
254 | ?>
255 |