27. Image SEO | SEO Forge - Rank Higher with AI-Powered SEO
Download Log in

27. Image SEO

Developer Guide

Automatic alt text generation and filename cleanup on image upload. These features help improve image accessibility and SEO without manual effort. PRO 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:

  1. Extract filename without extension
  2. Convert to lowercase
  3. Replace _ and spaces with -
  4. Remove all non-alphanumeric characters (except -)
  5. Collapse multiple consecutive dashes into one
  6. Trim leading/trailing dashes
Upload FilenameCleaned Filename
My Photo (1).JPGmy-photo-1.jpg
IMG_20240115_final.pngimg-20240115-final.png
Screenshot 2024.pngscreenshot-2024.png
DSC__0042---edit.jpegdsc-0042-edit.jpeg
product photo v2 FINAL.webpproduct-photo-v2-final.webp

Auto Alt Text Generation

On add_attachment hook, if the image has no alt text, SEO Forge generates one from the filename:

  1. Take the filename (without extension)
  2. Remove dimension suffixes (e.g. -800x600)
  3. Remove common suffixes (scaled, rotated, cropped, edited, copy, final, v1)
  4. Replace - _ . with spaces
  5. Apply MB_CASE_TITLE capitalization (Title Case)
  6. If the image has a parent post, append - {parent title}
php
// 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 ) );
php
// 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, SEO 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.

Forge AI Assistant Online

Hi! I'm the SEO Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs