2

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!

2

1 Answer 1

2

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.

4
  • For some reason it's not working for me. I wonder if it's because I'm using other modifications. Here's what I'm currently using: pokebeach.com/filters.txt
    – Jon
    Oct 27, 2013 at 8:10
  • yes the second filter your using is overriding it, it's also pretty bad.
    – Wyck
    Oct 27, 2013 at 14:56
  • Isn't the 2nd filter yours? XD
    – Jon
    Oct 27, 2013 at 18:00
  • The second image_send_to_editor filter....
    – Wyck
    Oct 27, 2013 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.