Get post embedded image caption

If I understood correctly, you are trying to add, to every image embedded in a post gallery, a custom rel attribute and a title attribute based on the attachment caption. Instead of parsing the content, you can try to modify the anchor that wraps the images using the 'wp_get_attachment_link' filter.

<?php

    function wpse221533_add_caption_title_to_content_gallery_image_attachments( $markup, $id, $size, $permalink, $icon, $text ) {

        //Target only images
        if ( ! wp_attachment_is_image( $id ) ) :
            return $markup;
        endif;

        //Limit the scope of the new attributes
        $post_types = array( 'post"https://wordpress.stackexchange.com/questions/221533/,"page' );

        if ( ! is_singular( $post_types ) ) :
            return $markup;
        endif;

        //Get attachment data
        $current_attachment_object = get_post( $id );

        //Get attachment caption
        $current_attachment_caption = $current_attachment_object->post_excerpt;

        //Nothing wrong with regex, but str_replace is cheaper
        $markup = str_replace( ' href="https://wordpress.stackexchange.com/questions/221533/," rel="lightbox" title="' . $current_attachment_caption . '" href=", $markup );

        return $markup;

    }

    add_filter( "wp_get_attachment_link"https://wordpress.stackexchange.com/questions/221533/,"wpse221533_add_caption_title_to_content_gallery_image_attachments', 10, 6 ) ;

?>

EDIT 16/04/06

In fact, I didn’t understand correctly, since I was referring to posts’ image galleries and the op to posts’ single image attachments. The code above will only work for galleries.

This other code will do it for single image attachments:

<?php

    function wpse221533_add_caption_title_to_content_single_image_attachments( $content ) {

        //Limit the scope of the new attributes
        $post_types = array( 'post"https://wordpress.stackexchange.com/questions/221533/,"page' );

        if ( ! is_singular( $post_types ) ) :
            return $content;
        endif;

        //Parse the content DOM for links
        $doc = new DOMDocument();
        $doc->loadHTML( $content );

        $anchors = $doc->getElementsByTagName( 'a' ); 

        //Parse each link
        foreach ( $anchors as $anchor ) :

            //Get the rel attribute
            $anchor_rel = $anchor->getAttribute( 'rel' );

            //Get the image ID based on the rel attribute
            $current_attachment_id = substr( strrchr( $anchor_rel, "-" ), 1 );

            //Check if the extracted ID is actually a number
            if ( ! is_numeric( $current_attachment_id ) ) :
                return $content;
            endif;

            //Target only images
            if ( ! wp_attachment_is_image( $current_attachment_id ) ) :
                return $content;
            endif;

            //Get the attachment object
            $current_attachment_object = get_post( $current_attachment_id );

            //Get the attachment caption
            $current_attachment_caption = $current_attachment_object->post_excerpt;

            //Set the rel attribute
            $anchor->setAttribute( 'rel"https://wordpress.stackexchange.com/questions/221533/,"lightbox' );

            //Finally set the title attribute based on the attachment caption
            $anchor->setAttribute( 'title', $current_attachment_caption );

        endforeach;

        //Save the DOM changes
        $content = $doc->saveHTML();

        return $content;

    }

    add_filter( 'the_content"https://wordpress.stackexchange.com/questions/221533/,"wpse221533_add_caption_title_to_content_single_image_attachments', 10, 1 );

?>

Leave a Comment