<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/* https://mekshq.com/change-image-alt-tag-in-wordpress/ */
function haup_force_fimage_alt_change( $html ) {
	$id = get_post_thumbnail_id();
	$at_alt_text = get_post_meta( $id, '_wp_attachment_image_alt', true );
	$post_title = get_the_title();

//メディア上にaltがあればそれを使用、なければ投稿タイトルを取得
	if(!empty($at_alt_text)){
		$alt_text = $at_alt_text;
	}else{
		$alt_text = get_the_title();
	}

if(haup_is_single() || is_page()){
	$html = preg_replace( '/(alt=")(.*?)(")/i', '$1'.esc_attr( $alt_text ).'$3', $html );
}

	return $html;

}
add_filter( 'post_thumbnail_html', 'haup_force_fimage_alt_change', 10, 5 );