Automatic alt text generation and filename cleanup on image upload. These features help improve image accessibility and SEO without manual effort. Free core feature.
Filename Cleanup Algorithm
Applied via the sanitize_file_name filter, this cleans up messy filenames before the file is saved to the uploads directory:
- Extract filename without extension
- Convert to lowercase
- Replace
_and spaces with- - Remove all non-alphanumeric characters (except
-) - Collapse multiple consecutive dashes into one
- Trim leading/trailing dashes
| Upload Filename | Cleaned Filename |
|---|---|
My Photo (1).JPG | my-photo-1.jpg |
IMG_20240115_final.png | img-20240115-final.png |
Screenshot 2024.png | screenshot-2024.png |
DSC__0042---edit.jpeg | dsc-0042-edit.jpeg |
product photo v2 FINAL.webp | product-photo-v2-final.webp |
Auto Alt Text Generation
On add_attachment hook, if the image has no alt text, Rank Forge generates one from the filename:
- Take the filename (without extension)
- Remove dimension suffixes (e.g.
-800x600) - Remove common suffixes (
scaled,rotated,cropped,edited,copy,final,v1) - Replace
-_.with spaces - Apply
MB_CASE_TITLEcapitalization (Title Case) - If the image has a parent post, append
- {parent title}
// Manually generate alt text for an existing image
$attachment_id = 123;
$filename = pathinfo( get_attached_file( $attachment_id ), PATHINFO_FILENAME );
$alt = str_replace( [ '-', '_', '.' ], ' ', $filename );
$alt = mb_convert_case( trim( preg_replace( '/s+/', ' ', $alt ) ), MB_CASE_TITLE );
$parent_id = wp_get_post_parent_id( $attachment_id );
if ( $parent_id ) {
$alt .= ' - ' . get_the_title( $parent_id );
}
update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $alt ) );// Bulk-generate alt text for all images missing it
$images = get_posts( [
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_query' => [ [ 'key' => '_wp_attachment_image_alt', 'compare' => 'NOT EXISTS' ] ],
] );
foreach ( $images as $img_id ) {
$filename = pathinfo( get_attached_file( $img_id ), PATHINFO_FILENAME );
$alt = str_replace( [ '-', '_', '.' ], ' ', $filename );
$alt = mb_convert_case( trim( preg_replace( '/s+/', ' ', $alt ) ), MB_CASE_TITLE );
$parent_id = wp_get_post_parent_id( $img_id );
if ( $parent_id ) {
$alt .= ' - ' . get_the_title( $parent_id );
}
update_post_meta( $img_id, '_wp_attachment_image_alt', sanitize_text_field( $alt ) );
}Missing Alt Text Warning
In the Media Library edit screen, Rank Forge displays a warning for images without alt text via the attachment_fields_to_edit filter. This visual indicator reminds editors to add descriptive alt text for accessibility and SEO.
—