alt, title tags not showing

I have added alt tags, title and description on all the images on my website but none are showing when I check them in the inspect element tool in chrome or firefox.

Here's a screenshot:

And here's the screenshot of inspect element of the same image:

Topic tags images html Wordpress

Category Web


It appears that (as of version 4.7) the alt attribute is no longer automatically displayed. There are plugins that restore this functionality (eg. https://wordpress.org/plugins/auto-image-alt/). If you have SEO in mind these are useful only to some extent because the attribute is added by JavaScript.


I found a script, try if this works for you. Add this in your theme's functions.php file

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 );

To make the alt tag appear with that image, you have to modify the theme. The code which outputs the image is located in mediatraining/inc/modules/trainers/shortcode.php. On line 13 the image is loaded form the options:

$img = mbtheme_get_option( 'lead-trainer-image' );
$img_url = $img[ 'url' ];

On line 27 it is used then:

<img src="<?php echo esc_url( $img_url ); ?>" />

So to be abnle to have the alt tag here you could try something like this on line 27:

<img src="<?php echo esc_url( $img_url ); ?>" alt="<?php echo esc_attr( $img['alt'] ); ?>" />

This is not tested and I am not sure about the $img['alt'] part, because I don't know what is exactly saved there. Alternatively try $img['title'] or try dumping $img to see if the alt text can be found in there.


It actually depends upon how the image is shown in the template. If it is shown using a function like the_post_thumbnail() which returns complete html string to show image with alt then there should be no problem but if image is shown by only fetching URL then it depends how it is displayed .

For example, below is snippet which is fetching image directly on a WP image attachment page:

//fetching attachment image src url 
$attachment_size = apply_filters( 'abc_attachment_size', array( 960, 960 ));
$image_attributes = wp_get_attachment_image_src( $post->ID, $attachment_size); 

if( $image_attributes ) {
?> 
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
<?php } ?>

In above code, we are displaying image from data we fetched individually, here since alt is not used , it will not show alt. Similarly in your case, I am assuming its because theme has no alt specified, its not being shown there.

Also, I tried downloading your theme file but was reported as malicious by browser so I left it.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.