I currently name all of my image files like this:

Earth the Blue Planet.jpg

Which when uploaded gets changed to this:

Earth-the-Blue-Planet.jpg

I was wondering if it is possible to have the image’s alt and title tags render like this using the filename when adding them to posts:

alt="Earth the Blue Planet" title="Earth the Blue Planet"

I know the title tag shouldn’t be used like that, but I’ll just be using the filename as a base to add in the information I want. This would just make it easier.

Thank you so much! I love this place for all my WordPress questions!

1 Answer
1

The alt tag already takes the filename but if for some reason you need to replace hyphens with spaces and include a duplicate title of the alt tag you can do something like:

function wpse_120228_seomadness($html, $id, $caption, $title, $align, $url, $size, $alt) {

    $alttitle = str_replace('-', ' ', $alt);
    $img      = get_image_tag($id, $alttitle, $alttitle, $align, $size);
    $html="<a href="" . esc_attr($url) . '">' . $img . '</a>';

    return $html;
}
add_filter( 'image_send_to_editor', 'wpse_120228_seomadness', 10, 9 );

This will only affect images inserted into the editor after the code is added and not images already on your site.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *