Auto Image Attributes From Filename With Bulk Updater (Add Alt Text, Image Title For Image SEO) - Version 1.1

Version Description

  • Added options to choose individual image attributes for NEW uploads.

=

Download this release

Release Info

Developer arunbasillal
Plugin Icon 128x128 Auto Image Attributes From Filename With Bulk Updater (Add Alt Text, Image Title For Image SEO)
Version 1.1
Comparing to
See all releases

Code changes from version 1.0 to 1.1

Files changed (2) hide show
  1. iaff_image-attributes-from-filename.php +100 -40
  2. readme.txt +15 -5
iaff_image-attributes-from-filename.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://millionclues.com/portfolio/
5
  Description: Automatically Add Image Title, Image Caption, Description And Alt Text From Image Filename. Since this plugin includes a bulk updater this can update both existing images in the Media Library and new images.
6
  Author: Arun Basil Lal
7
  Author URI: http://millionclues.com
8
- Version: 1.0
9
  Text Domain: abl_iaff_td
10
  Domain Path: /languages
11
  License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
@@ -63,8 +63,7 @@ add_action( 'plugins_loaded', 'iaff_load_plugin_textdomain' );
63
 
64
  // Do Stuff On Plugin Activation
65
  function iaff_activate_plugin() {
66
- add_option( 'iaff_image_attributes_from_filename_settings', '1' ); // Setting default value. 1 = enable Auto Image Attributes for new images on plugin install
67
- add_option( 'iaff_bulk_updater_counter', '0' ); // Setting numer of images processed as zero
68
  }
69
  register_activation_hook( __FILE__, 'iaff_activate_plugin' );
70
 
@@ -73,32 +72,37 @@ register_activation_hook( __FILE__, 'iaff_activate_plugin' );
73
  function iaff_register_settings() {
74
 
75
  // Register Setting
76
- register_setting( 'iaff_image_attributes_from_filename_settings_group', 'iaff_image_attributes_from_filename_settings', 'boolval' );
 
 
 
77
 
78
  // Register A New Section
79
  add_settings_section(
80
- 'iaff_auto_image_attributes_settings', // ID
81
- __('Auto Image Attributes', 'abl_iaff_td'), // Title
82
- 'iaff_auto_image_attributes_callback', // Callback Function
83
- 'image-attributes-from-filename' // Page slug
84
  );
85
 
86
- // Register a new field in the "iaff_auto_image_attributes_settings" section, inside the "image-attributes-from-filename" page
87
  add_settings_field(
88
- 'iaff_auto_image_attributes_settings_field', // ID
89
- __('Auto Image Attributes Setting', 'abl_iaff_td'), // Title
90
  'iaff_auto_image_attributes_settings_field_callback', // Callback function
91
  'image-attributes-from-filename', // Page slug
92
  'iaff_auto_image_attributes_settings' // Settings Section ID
93
  );
 
94
  }
95
  add_action( 'admin_init', 'iaff_register_settings' );
96
 
97
 
98
  // Do Stuff On Plugin Uninstall
99
  function iaff_uninstall_plugin() {
100
- delete_option( 'iaff_image_attributes_from_filename_settings' );
101
  delete_option( 'iaff_bulk_updater_counter' );
 
102
  }
103
  register_uninstall_hook(__FILE__, 'iaff_uninstall_plugin' );
104
 
@@ -108,20 +112,49 @@ register_uninstall_hook(__FILE__, 'iaff_uninstall_plugin' );
108
  /* Admin Options Page */
109
  /*--------------------------------------*/
110
 
111
- function iaff_auto_image_attributes_callback()
112
- {
113
- echo '<p>' . __('Enable this to automatically add Image Caption, Description and Alt Text from image title for new uploads.', 'abl_iaff_td') . '</p>';
114
  }
115
 
116
- // field content cb
117
- function iaff_auto_image_attributes_settings_field_callback()
118
- {
119
- // Get the value of the setting we've registered with register_setting()
120
- $setting = get_option('iaff_image_attributes_from_filename_settings');
 
 
 
 
 
121
 
122
- // Output the field
123
- // ID and name of form element should be same as the setting name. ?>
124
- <input type="checkbox" name="iaff_image_attributes_from_filename_settings" id="iaff_image_attributes_from_filename_settings" value="Enable Auto Image Attributes" <?php echo boolval($setting) ? 'checked' : '';?>><label for="iaff_image_attributes_from_filename_settings"><?php _e('Enable Auto Image Attributes', 'abl_iaff_td') ?></label>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  <?php
127
  }
@@ -151,7 +184,7 @@ function iaff_admin_interface_render () {
151
  <form action="options.php" method="post">
152
  <?php
153
  // Output nonce, action, and option_page fields for a settings page.
154
- settings_fields( 'iaff_image_attributes_from_filename_settings_group' );
155
 
156
  // Prints out all settings sections added to a particular settings page.
157
  do_settings_sections( 'image-attributes-from-filename' ); // Page slug
@@ -165,8 +198,8 @@ function iaff_admin_interface_render () {
165
 
166
  <p style="color:red"><?php _e('IMPORTANT: Please backup your database before running the bulk updater.', 'abl_iaff_td') ?></p>
167
  <p><?php _e('Run this bulk updater to update Image Title, Caption, Description and Alt Text from image filename for existing images in the media library.', 'abl_iaff_td') ?></p>
168
- <p><?php _e('If your image is named a-lot-like-love.jpg, your Image Title, Caption, Description and Alt Text will be: A Lot Like Love.', 'abl_iaff_td') ?></p>
169
- <p><?php _e('Do not close the browser while it\'s running. In case you do, you can always resume by returning to this page later.', 'abl_iaff_td') ?></p> <?php
170
 
171
  submit_button( __('Run Bulk Updater', 'abl_iaff_td'), 'iaff_run_bulk_updater_button' ); ?>
172
 
@@ -189,28 +222,39 @@ function iaff_admin_interface_render () {
189
  /*--------------------------------------*/
190
 
191
 
192
- // Auto Add Image Attributes From Image Filename
193
  function iaff_auto_image_attributes( $post_ID ) {
194
 
195
- // Check if Auto Image Attributes is enabled
196
- $setting = get_option('iaff_image_attributes_from_filename_settings');
197
- if ( ! boolval($setting) ) {
198
- return;
199
- }
 
 
 
 
200
 
201
  $attachment = get_post( $post_ID );
202
  $attachment_title = $attachment->post_title;
203
-
204
  $attachment_title = str_replace( '-', ' ', $attachment_title ); // Hyphen Removal
205
  $attachment_title = ucwords( $attachment_title ); // Capitalize First Word
206
 
207
  $uploaded_image = array();
208
  $uploaded_image['ID'] = $post_ID;
209
- $uploaded_image['post_title'] = $attachment_title; // Image Title
210
- $uploaded_image['post_excerpt'] = $attachment_title; // Image Caption
211
- $uploaded_image['post_content'] = $attachment_title; // Image Description
212
 
213
- update_post_meta( $post_ID, '_wp_attachment_image_alt', $attachment_title ); // Image Alt Text
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  wp_update_post( $uploaded_image );
216
 
@@ -246,8 +290,8 @@ function iaff_rename_old_image() {
246
 
247
  // Update the image Title, Caption and Description with the image name
248
  $updated_image = array(
249
- 'ID' => $image->ID,
250
- 'post_title' => $image_name, // Image Title
251
  'post_excerpt' => $image_name, // Image Caption
252
  'post_content' => $image_name, // Image Description
253
  );
@@ -283,7 +327,7 @@ function iaff_total_number_of_images() {
283
  return $total_no_of_images;
284
  }
285
 
286
- // Count Remaining Number Of Images To Process
287
  function iaff_count_remaining_images() {
288
 
289
  $total_no_of_images = iaff_total_number_of_images();
@@ -386,4 +430,20 @@ function iaff_image_bulk_updater() {
386
  </script> <?php
387
  }
388
  add_action( 'admin_footer', 'iaff_image_bulk_updater' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  ?>
5
  Description: Automatically Add Image Title, Image Caption, Description And Alt Text From Image Filename. Since this plugin includes a bulk updater this can update both existing images in the Media Library and new images.
6
  Author: Arun Basil Lal
7
  Author URI: http://millionclues.com
8
+ Version: 1.1
9
  Text Domain: abl_iaff_td
10
  Domain Path: /languages
11
  License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
63
 
64
  // Do Stuff On Plugin Activation
65
  function iaff_activate_plugin() {
66
+ add_option( 'iaff_bulk_updater_counter', '0' ); // Setting numer of images processed as zero
 
67
  }
68
  register_activation_hook( __FILE__, 'iaff_activate_plugin' );
69
 
72
  function iaff_register_settings() {
73
 
74
  // Register Setting
75
+ register_setting(
76
+ 'iaff_settings_group', // Group Name
77
+ 'iaff_settings' // Setting Name = HTML form <input> name on settings form
78
+ );
79
 
80
  // Register A New Section
81
  add_settings_section(
82
+ 'iaff_auto_image_attributes_settings', // ID
83
+ __('Auto Image Attributes For New Uploads', 'abl_iaff_td'), // Title
84
+ 'iaff_auto_image_attributes_callback', // Callback Function
85
+ 'image-attributes-from-filename' // Page slug
86
  );
87
 
88
+ // Setting: Enable Auto Image Attributes
89
  add_settings_field(
90
+ 'iaff_general_settings', // ID
91
+ __('General Settings', 'abl_iaff_td'), // Title
92
  'iaff_auto_image_attributes_settings_field_callback', // Callback function
93
  'image-attributes-from-filename', // Page slug
94
  'iaff_auto_image_attributes_settings' // Settings Section ID
95
  );
96
+
97
  }
98
  add_action( 'admin_init', 'iaff_register_settings' );
99
 
100
 
101
  // Do Stuff On Plugin Uninstall
102
  function iaff_uninstall_plugin() {
103
+ delete_option( 'iaff_settings' );
104
  delete_option( 'iaff_bulk_updater_counter' );
105
+ delete_option( 'iaff_image_attributes_from_filename_settings' ); // Used in Ver 1.0 of the plugin. Simpler days.
106
  }
107
  register_uninstall_hook(__FILE__, 'iaff_uninstall_plugin' );
108
 
112
  /* Admin Options Page */
113
  /*--------------------------------------*/
114
 
115
+ function iaff_auto_image_attributes_callback() {
116
+ echo '<p>' . __('Automatically add Image attributes such as Image Title, Image Caption, Description And Alt Text from Image Filename for new uploads.', 'abl_iaff_td') . '</p>';
 
117
  }
118
 
119
+ // Settings Field Callback
120
+ function iaff_auto_image_attributes_settings_field_callback() {
121
+
122
+ // Default Values For Settings
123
+ $defaults = array(
124
+ 'image_title' => '1',
125
+ 'image_caption' => '1',
126
+ 'image_description' => '1',
127
+ 'image_alttext' => '1'
128
+ );
129
 
130
+ // Get Settings
131
+ $settings = get_option('iaff_settings', $defaults);
132
+
133
+ // General Settings. Name of form element should be same as the setting name in register_setting(). ?>
134
+
135
+ <!-- Auto Add Image Title -->
136
+ <input type="checkbox" name="iaff_settings[image_title]" id="iaff_settings[image_title]" value="1"
137
+ <?php if ( isset( $settings['image_title'] ) ) { checked( '1', $settings['image_title'] ); } ?>>
138
+ <label for="iaff_settings[image_title]"><?php _e('Set Image Title from filename for new uploads', 'abl_iaff_td') ?></label>
139
+ <br>
140
+
141
+ <!-- Auto Add Image Caption -->
142
+ <input type="checkbox" name="iaff_settings[image_caption]" id="iaff_settings[image_caption]" value="1"
143
+ <?php if ( isset( $settings['image_caption'] ) ) { checked( '1', $settings['image_caption'] ); } ?>>
144
+ <label for="iaff_settings[image_caption]"><?php _e('Set Image Caption from filename for new uploads', 'abl_iaff_td') ?></label>
145
+ <br>
146
+
147
+ <!-- Auto Add Image Description -->
148
+ <input type="checkbox" name="iaff_settings[image_description]" id="iaff_settings[image_description]" value="1"
149
+ <?php if ( isset( $settings['image_description'] ) ) { checked( '1', $settings['image_description'] ); } ?>>
150
+ <label for="iaff_settings[image_description]"><?php _e('Set Image Description from filename for new uploads', 'abl_iaff_td') ?></label>
151
+ <br>
152
+
153
+ <!-- Auto Add Alt Text -->
154
+ <input type="checkbox" name="iaff_settings[image_alttext]" id="iaff_settings[image_alttext]" value="1"
155
+ <?php if ( isset( $settings['image_alttext'] ) ) { checked( '1', $settings['image_alttext'] ); } ?>>
156
+ <label for="iaff_settings[image_alttext]"><?php _e('Set Image Alt Text from filename for new uploads', 'abl_iaff_td') ?></label>
157
+ <br>
158
 
159
  <?php
160
  }
184
  <form action="options.php" method="post">
185
  <?php
186
  // Output nonce, action, and option_page fields for a settings page.
187
+ settings_fields( 'iaff_settings_group' );
188
 
189
  // Prints out all settings sections added to a particular settings page.
190
  do_settings_sections( 'image-attributes-from-filename' ); // Page slug
198
 
199
  <p style="color:red"><?php _e('IMPORTANT: Please backup your database before running the bulk updater.', 'abl_iaff_td') ?></p>
200
  <p><?php _e('Run this bulk updater to update Image Title, Caption, Description and Alt Text from image filename for existing images in the media library.', 'abl_iaff_td') ?></p>
201
+ <p><?php _e('If your image is named a-lot-like-love.jpg, your Image Title, Caption, Description and Alt Text will be: A Lot Like Love. All attributes are updated regardless of the settings for NEW uploads.', 'abl_iaff_td') ?></p>
202
+ <p><?php _e('Be Patient and do not close the browser while it\'s running. In case you do, you can always resume by returning to this page later.', 'abl_iaff_td') ?></p> <?php
203
 
204
  submit_button( __('Run Bulk Updater', 'abl_iaff_td'), 'iaff_run_bulk_updater_button' ); ?>
205
 
222
  /*--------------------------------------*/
223
 
224
 
225
+ // Auto Add Image Attributes From Image Filename For New Uploads
226
  function iaff_auto_image_attributes( $post_ID ) {
227
 
228
+ // Default Values For Settings
229
+ $defaults = array(
230
+ 'image_title' => '1',
231
+ 'image_caption' => '1',
232
+ 'image_description' => '1',
233
+ 'image_alttext' => '1'
234
+ );
235
+ // Get Settings
236
+ $settings = get_option('iaff_settings', $defaults);
237
 
238
  $attachment = get_post( $post_ID );
239
  $attachment_title = $attachment->post_title;
 
240
  $attachment_title = str_replace( '-', ' ', $attachment_title ); // Hyphen Removal
241
  $attachment_title = ucwords( $attachment_title ); // Capitalize First Word
242
 
243
  $uploaded_image = array();
244
  $uploaded_image['ID'] = $post_ID;
 
 
 
245
 
246
+ if ( boolval($settings['image_title']) ) {
247
+ $uploaded_image['post_title'] = $attachment_title; // Image Title
248
+ }
249
+ if ( boolval($settings['image_caption']) ) {
250
+ $uploaded_image['post_excerpt'] = $attachment_title; // Image Caption
251
+ }
252
+ if ( boolval($settings['image_description']) ) {
253
+ $uploaded_image['post_content'] = $attachment_title; // Image Description
254
+ }
255
+ if ( boolval($settings['image_alttext']) ) {
256
+ update_post_meta( $post_ID, '_wp_attachment_image_alt', $attachment_title ); // Image Alt Text
257
+ }
258
 
259
  wp_update_post( $uploaded_image );
260
 
290
 
291
  // Update the image Title, Caption and Description with the image name
292
  $updated_image = array(
293
+ 'ID' => $image->ID,
294
+ 'post_title' => $image_name, // Image Title
295
  'post_excerpt' => $image_name, // Image Caption
296
  'post_content' => $image_name, // Image Description
297
  );
327
  return $total_no_of_images;
328
  }
329
 
330
+ // Print Remaining Number Of Images To Process
331
  function iaff_count_remaining_images() {
332
 
333
  $total_no_of_images = iaff_total_number_of_images();
430
  </script> <?php
431
  }
432
  add_action( 'admin_footer', 'iaff_image_bulk_updater' );
433
+
434
+
435
+ // Admin Footer Text - Ask For 5 Star Rating.
436
+ function iaff_footer_text($default) {
437
+
438
+ // Retun Default On Non-plugin Pages
439
+ $screen = get_current_screen();
440
+ if ( $screen->id !== "settings_page_image-attributes-from-filename" ) {
441
+ return $default;
442
+ }
443
+
444
+ $iaff_footer_text = sprintf( __( 'If you like this plugin please leave me a %s rating. Thanks a bunch!', 'abl_iaff_td' ), '<a href="https://wordpress.org/support/plugin/auto-image-attributes-from-filename-with-bulk-updater/reviews/?rate=5#new-post" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>' );
445
+
446
+ return $iaff_footer_text;
447
+ }
448
+ add_filter('admin_footer_text', 'iaff_footer_text');
449
  ?>
readme.txt CHANGED
@@ -18,9 +18,9 @@ The plugin can update image attributes for both new images and existing images i
18
 
19
  If your image filename is *my-image-name.jpg*, your Image Title, Caption, Description And Alt Text will be *My Image Name*.
20
 
21
- The bulk updater and plugin settings in WordPress Admin > Settings > Image Attributes. You can disable auto attributes for new uploads by disabling it in the settings page.
22
 
23
- Please remember to take a database backup before running the bulk updater.
24
 
25
  == Installation ==
26
 
@@ -28,13 +28,17 @@ To install this plugin:
28
 
29
  1. Install the plugin through the WordPress admin interface, or upload the plugin folder to /wp-content/plugins/ using ftp.
30
  2. Activate the plugin through the 'Plugins' screen in WordPress.
31
- 3. Go to WordPress Admin > Settings > Image Attributes
32
 
33
  == Frequently Asked Questions ==
34
 
35
  = Will this plugin update existing images in the media library? =
36
 
37
- Yes, the plugin will update Image Title, Caption, Description And Alt Text from the imgage filename for both existing images in the media library and new uploads.
 
 
 
 
38
 
39
  == Screenshots ==
40
 
@@ -45,7 +49,13 @@ Yes, the plugin will update Image Title, Caption, Description And Alt Text from
45
  = 1.0 =
46
  * First release of the plugin.
47
 
 
 
 
48
  == Upgrade Notice ==
49
 
50
  = 1.0 =
51
- * First release of the plugin.
 
 
 
18
 
19
  If your image filename is *my-image-name.jpg*, your Image Title, Caption, Description And Alt Text will be *My Image Name*.
20
 
21
+ The plugin settings and bulk updater are in WordPress Admin > Settings > Image Attributes. Here you can choose which attributes to update for NEW uploads.
22
 
23
+ **Please remember to take a database backup before running the bulk updater. The bulk updater updates ALL attributes for existing images in the Media Library and ignores the settings set for NEW uploads.**
24
 
25
  == Installation ==
26
 
28
 
29
  1. Install the plugin through the WordPress admin interface, or upload the plugin folder to /wp-content/plugins/ using ftp.
30
  2. Activate the plugin through the 'Plugins' screen in WordPress.
31
+ 3. Go to WordPress Admin > Settings > Image Attributes.
32
 
33
  == Frequently Asked Questions ==
34
 
35
  = Will this plugin update existing images in the media library? =
36
 
37
+ Yes, the plugin will update image Title, Caption, Description And Alt Text from the image filename for both existing images in the media library and new uploads.
38
+
39
+ = I need more features. Can I hire you? =
40
+
41
+ Yes! Please [get in touch via my contact form](http://millionclues.com/contact/) with a brief description of your requirement and budget for the project. I will be in touch shortly.
42
 
43
  == Screenshots ==
44
 
49
  = 1.0 =
50
  * First release of the plugin.
51
 
52
+ = 1.1 =
53
+ * Added options to choose individual image attributes for NEW uploads.
54
+
55
  == Upgrade Notice ==
56
 
57
  = 1.0 =
58
+ * First release of the plugin.
59
+
60
+ = 1.0 =
61
+ * New feature. Now you can choose specific image attributes for NEW uploads.