Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1492366

    I was looking for a way to display the image copyright information (in addition to the original image’s caption) when I place media in a text element. (Sadly, neither are available, only a custom caption.) Please add these options when placing media in a text element.

    I considered that using the image element (though this would create lots of work for me), might be a work around. I was happy to see that the image copyright was available, but strangely not the image caption.

    The dynamic field capability (thank you, very nice) does not allow me to choose the image caption.

    I was attempting to add this field in a custom layout. I tried in a regular page, same issue.

    Any way to make this consistent across the board so anywhere you place an image, you can use the values in the fields that are already associated with the image (caption, description, copyright, title)???

    #1492482

    Hey milkrow,
    Thanks for your patience, if I understand correctly you are adding images in the text element in the ALB (advanced layout builder):
    fYB7rrX.md.png
    and instead of just the caption showing below the image, you want caption, description, copyright, and title to show like this:
    fYBExaI.md.png
    In my tests this snippet in your child theme functions.php works:

    add_filter('the_content', 'enfold_add_image_metadata');
    
    function enfold_add_image_metadata($content) {
        // Only process on frontend
        if (is_admin()) {
            return $content;
        }
        
        // Pattern to match images with WordPress attachment IDs
        $pattern = '/<img[^>]+wp-image-(\d+)[^>]*>/i';
        
        preg_match_all($pattern, $content, $matches);
        
        if (!empty($matches[0])) {
            foreach ($matches[0] as $index => $img_tag) {
                $attachment_id = $matches[1][$index];
                
                // Get image metadata
                $title = get_the_title($attachment_id);
                $caption = wp_get_attachment_caption($attachment_id);
                $description = get_post_field('post_content', $attachment_id);
                $copyright = get_post_meta($attachment_id, '_avia_attachment_copyright', true);
                
                // Build metadata HTML
                $metadata_html = '';
                
                if ($title || $caption || $description || $copyright) {
                    $metadata_html .= '<div class="image-metadata" style="line-height: 14px; color: #666;">';
                    
                    if ($title) {
                        $metadata_html .= '<div class="image-title" style="font-weight: bold;">' . esc_html($title) . '</div>';
                    }
                    
                    if ($caption) {
                        $metadata_html .= '<div class="image-caption" style="font-style: italic;">' . esc_html($caption) . '</div>';
                    }
                    
                    if ($description) {
                        $metadata_html .= '<div class="image-description">' . wp_kses_post($description) . '</div>';
                    }
                    
                    if ($copyright) {
                        $metadata_html .= '<div class="image-copyright">© ' . esc_html($copyright) . '</div>';
                    }
                    
                    $metadata_html .= '</div>';
                }
                
                // Replace the image with image + metadata
                if ($metadata_html) {
                    $replacement = $img_tag . $metadata_html;
                    $content = str_replace($img_tag, $replacement, $content);
                }
            }
        }
        
        return $content;
    }
    
    /**
     * Add CSS to hide default WordPress caption text
     * while keeping the caption container styling
     */
    add_action('wp_head', 'enfold_hide_default_caption_text');
    
    function enfold_hide_default_caption_text() {
        ?>
        <style>
            /* Hide the default WordPress caption text inside caption shortcode */
            .wp-caption p {
                display: none !important;
            }
        </style>
        <?php
    }

    In your media library add the text to your image:
    fYBwZjS.md.png

    Best regards,
    Mike

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.