function wpdocs_replaceALT( $content ) {
if ( is_front_page() ) {
libxml_use_internal_errors( true );
$post = new DOMDocument();
$post->loadHTML( $content );
$images = $post->getElementsByTagName( 'img' );
foreach ( $images as $image ) {
if ( !empty( $image->getAttribute( 'alt' ) ) ) {
$src = $image->getAttribute( 'src' );
$alt = pathinfo( $src, PATHINFO_FILENAME );
$image->setAttribute( 'alt', $alt );
$image->setAttribute( 'data-src', $alt );
}
}
$content = $post->saveHTML();
return $content;
}
}
add_filter( 'the_content', 'wpdocs_replaceALT' );