• Hi all- I inherited a site with an older theme and I’ve been working on getting its accessibility up to speed. I entered alt text for every image on the site, but it’s not showing up in the sourcecode. I believe this is due to the theme. I looked in functions.php and there’s nothing there that calls to images other than thumbnails. Would I be correct in assuming I need to add a script to functions to force images to show the alt text? PHP is not my forte, and while I can fumble my way through it, I’ve yet to find any options that really make sense for this issue. Below is the only possible option I’ve found so far…but have no clue what the 10 and 2 are for.
    Thanks!

    function isa_add_img_title( $attr, $attachment = null ) {
    
        $img_title = trim( strip_tags( $attachment->post_title ) );
    
        $attr['title'] = $img_title;
        $attr['alt'] = $img_title;
    
        return $attr;
    }
    add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    https://developer.www.ads-software.com/reference/functions/add_filter/#parameters
    10, 2 are $priority and $accepted_args

    Your code could work for newly inserted images or those dynamically served like featured images. It depends on how the theme manages img tag output. What might be the problem are img tags in existing content. If they have no alt attributes now, adding alt text to the image’s attachment record will not impact existing img tags in post content. To address that situation, you can either filter content on output and insert an alt attribute where necessary; or create a custom one time script to update existing content in the DB.

    Thread Starter toxoplasmaarts

    (@toxoplasmaarts)

    I see! Most images had alt attributes already, and I only had to add about 20-30 missing ones during my audit. Would I remove the priority tags in that case? There are no posts- the site doesn’t have a blog.

    Moderator bcworkz

    (@bcworkz)

    If you want your code to still function, let the priority and arg count parameters remain, otherwise collecting $attachment in isa_add_img_title() will not work. If you want to disable the code but are not ready to fully remove it, make the entire add_filter() line into a comment.

    Changing alt tags does not affect those embedded in any sort of post content field, including that of pages and any other post types that utilize img tags within the post content field. It’s not unique to only blog posts.

    • This reply was modified 4 years, 1 month ago by bcworkz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Img Alt Attribute Not Appearing in Sourcecode’ is closed to new replies.