Automatically Add ALT name on Thumbnail | WordPress

Want to automatic add ALT name on uploaded image on WordPress? So this article helpful for you, Open your thene and paste given code and apply or save theme.

How to Do

1st method

Find out has_post_thumbnail() on index.php file. Then add [ ‘alt’ => esc_html ( get_the_title() ) ] . So now see all code of my WordPress theme for better understand.

My WordPress’s theme code

if( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute() ?>">
<?php the_post_thumbnail( 'thumbnail', [ 'alt' => esc_html ( get_the_title() ) ] ); ?>
</div> <?php }

2nd MethodPaste this code on functions.php file and SAVE.

// Automatic image ALT name
function auto_image_alt_name_thekroyaard( $post_ID ) {
if ( wp_attachment_is_image( $post_ID ) ) {
$auto_alt_image_title = get_post( $post_ID )->post_title;
update_post_meta( $post_ID, '_wp_attachment_image_alt', $auto_alt_image_title );
add_action( 'add_attachment', 'auto_image_alt_name_thekroyaard' );

Hope you can understand and add this code on your custom theme. Thanks for reading this article.