Skip to content

Instantly share code, notes, and snippets.

@algmelo
Created September 4, 2024 18:02
Show Gist options
  • Save algmelo/8092178652e7613e886af47cff7e3619 to your computer and use it in GitHub Desktop.
Save algmelo/8092178652e7613e886af47cff7e3619 to your computer and use it in GitHub Desktop.
Altera a saída do block image, para adicionar o custom_credit
public function __construct()
{
add_filter('render_block', array($this, 'render_custom_image'), 10, 2);
}
public function render_custom_image($block_content, $block)
{
if ($block['blockName'] === 'core/image') {
$image_id = $block['attrs']['id'] ?? null;
if ($image_id) {
$custom_credit = get_post_meta($image_id, 'custom_credit', true);
if (!empty($custom_credit)) {
$custom_credit_html = sprintf('<span class="custom-credit">%s %s</span>', __('Credit:', BOOTSTRAPTHEME_TEXTDOMAIN), esc_html($custom_credit));
if (strpos($block_content, '<figcaption class="wp-element-caption">') !== false) {
$block_content = str_replace('</figcaption>', ' ' . $custom_credit_html . '</figcaption>', $block_content);
} else {
$block_content .= '<figcaption class="wp-element-caption">' . $custom_credit_html . '</figcaption>';
}
}
}
}
return $block_content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment