How to add alternative text (alt tag/alt text) to the featured image in Tribe Events Calendar’s REST API response.

As of March 15th, 2022, the (WordPress plugin) Tribe Events Calendar’s REST API response for an event does not include the alt text for the featured image.

Alt text is an important part of the image REST response, so here’s how to add it:

add_filter('tribe_rest_event_featured_image', 'wch_add_alt_text_featured_image_tribe_rest_api', 10, 2);
function wch_add_alt_text_featured_image_tribe_rest_api($data, $post) {
    if($data['id'] && !array_key_exists('alt_text', $data)) {
        $data['alt_text'] = get_post_meta( $data['id'], '_wp_attachment_image_alt', true );
    }
  
    return $data;
}

The filter being used is found in the Tribe Event Calendar’s Post_Repository.php file:

\the-events-calendar\src\Tribe\REST\V1\Post_Repository.php

Leave a Reply

Your email address will not be published. Required fields are marked *