Support » Fixing WordPress » Automatically add Alt Tag to WordPress Image Uploads

  • Resolved brennenlb


    Hi! This is my first post. I read the forum welcome, but odds are I’m going to screw something up anyway so cut me some slack!

    What I want to do:
    When uploading media to WordPress via wp-admin, the title of the media (image in this case) is automatically pulled from the uploaded file and placed into the “title” field. I’m developing a listing site where users are able to import their own photos but I don’t expect them to insert an alt tag (because most of them wont know what that is). I’d rather have the field populate as a duplicate of the title field. So, let’s say the upload is “Camp-fire.png.” WordPress would automatically fill in the following:

    • Title: camp-fire
    • Alt Tag: camp-fire

    I know there are some plugins out there that do this but I’m trying to avoid plugins as much as possible as I already have too many.

    Does anybody have any suggestions for PHP functions? Thanks so much! (WP 4.5.3 on a 4GB ram DigitalOcean droplet under git Version Control)

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello brennenlb
    You can easily populate these fields by getting names of uploaded images through their path like if uploaded path is “abc/def/example.png” then you can easily get “example” from the PHP function basename(“abc/def/example.png”,”.png”) and after getting name of images you can pouplate these fields.You can also read about this function from here
    Hope this will help you

    Thread Starter brennenlb


    @cedcommerce Thanks for your answer! Do you mind pointing me in the direction of the file that I should edit? I assume it would be in the WordPress webroot directory, rather than the theme directory. Thanks

    Hello brennenlb
    After doing some research i have got a solution for you ,so give it a try.Put the code below in your theme’s functions.php and try uploading again

    add_action( 'add_attachment', 'ced_add_image_meta_data' );
    function ced_add_image_meta_data( $attachment_ID ) {
     $filename   =   $_REQUEST['name']; // or get_post by ID
     $withoutExt =   preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
     $withoutExt =   str_replace(array('-','_'), ' ', $withoutExt);
     $my_post = array(
       'ID'           => $attachment_ID,
       'post_excerpt' => $withoutExt,  // caption
       'post_content' => $withoutExt,  // description
     wp_update_post( $my_post );
     // update alt text for post
     update_post_meta($attachment_ID, '_wp_attachment_image_alt', $withoutExt );

    Hope this time you will mark it resolved.

    Thread Starter brennenlb


    That worked perfectly! Thank you so much for your help!

    Yes, I’d like to second that @cedcommerce. Been trying to find this solution for a couple hours and your code fixed me up. Cheers.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Automatically add Alt Tag to WordPress Image Uploads’ is closed to new replies.