| Server IP : 3.147.158.171 / Your IP : 216.73.216.216 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /tsai/repo/sites/dev/wp-content/plugins/tsai-plugin/includes/ |
Upload File : |
<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* Featured-image caption rendering.
*
* The publishing pipeline stores a caption on the featured attachment
* (post_excerpt) via the WP media REST endpoint. The block-theme
* `core/post-featured-image` block renders only the <img> and ignores that
* caption, so it would never show. This filter appends a <figcaption> built
* from the attachment caption whenever a single post's featured image is
* rendered, giving it the same visible treatment as the inline secondary
* image's caption.
*
* The tsai-plugin is installed on every tsai + scike site, so this is the
* single place that controls featured-caption output for all sites.
*/
class TSAI_Featured_Caption {
public function __construct() {
add_filter('render_block', array($this, 'append_caption'), 10, 2);
add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
}
/**
* Append the featured image's caption as a <figcaption> inside the block.
*
* @param string $block_content Rendered block HTML.
* @param array $block Parsed block (name + attrs).
* @return string
*/
public function append_caption($block_content, $block) {
if (empty($block['blockName']) || $block['blockName'] !== 'core/post-featured-image') {
return $block_content;
}
if (!is_singular('post') || empty($block_content)) {
return $block_content;
}
$caption = get_the_post_thumbnail_caption(get_queried_object_id());
if (!$caption) {
return $block_content;
}
$figcaption = '<figcaption class="wp-element-caption tsai-featured-caption">'
. esc_html($caption) . '</figcaption>';
// Append the caption AFTER the block's <figure>, not inside it. Block
// themes size the featured-image figure with `aspect-ratio` and an
// `img { height: 100% }`, so a caption placed inside overflows that
// fixed-ratio box and gets painted over by the following content —
// present in the DOM but invisible. As a sibling in normal flow it
// occupies its own space directly under the image.
return $block_content . $figcaption;
}
public function enqueue_assets() {
if (!is_singular('post')) {
return;
}
wp_enqueue_style(
'tsai-featured-caption',
TSAI_PLUGIN_URL . 'assets/css/tsai-featured-caption.css',
array(),
'1.0.0'
);
}
}
new TSAI_Featured_Caption();