• Hello,
    We have thousands of images with a caption in our pages. We need to extract/save those captions as the image description (or even better Exif data) in bulk so that we can organise all those images by that Exif/description data (with tags etc) (frontend and backend) (yeah, better late then never ?? ).
    (In turn we can show the description then as a caption and the result would look the same, but then we can tag & structure everything)

    [caption id="attachment_15350" align="alignnone" width="445"]<a href="IMAGEURL"><img class="wp-image-15350 size-medium" src="IMAGEURL" alt="" width="445" height="340" /></a>Abraham Patrasstraat De Carpentierstraat - Haags Gemeentearchief[/caption]
    As far as I know the caption data in the text editor is not stored together with the Image attachment in any way? It’s just a piece of text in the page?

    So… is saving that caption text possible and if so, how?

    Or should I do it on databaselevel? (something like; Export database –> create a list of images with it’s captions –> split columns –> update the database with a list of images with descriptions?). Or is an SQL statement easier/possible?

    any help is highly appreciated,
    kind regards,
    Harold Vogelaar

Viewing 1 replies (of 1 total)
  • Anonymous User 14808221

    (@anonymized-14808221)

    The caption when used in the shortcode you mention (also explained here) is indeed just text on the page wrapped in the ShortCode, so to save that to the actual Image/Attachment itself you would need quite elaborated Custom Code which we cannot provide in this forum (it would be quite above the scope of the volunteers forum), as it would require – just as one possible approach – scanning the post body on save_post() action, locate the shortcode, inside it locate the Attachment ID, then get the “caption” text, and update the attachment with that data.

    Potentially where will be more than one such ShortCode on page, hence complexing the code a little more.

    IF you want to go that road, you would – apart of how to get the data from the post body – also be looking at how to update the actual attachment.

    Here you’d need to know that the attachment will be a post, of type attachment, saved in the posts table.
    The Caption, will be the post_excerpt.
    The Description, will be the post_content.
    The Title, will be the post_title.

    The ALT attribute instead, will not be in the posts object. It will be a Post Meta, with key _wp_attachment_image_alt connected to the attachment post by the post ID.

    This information will help you to update the data to the attachment post.

    However, as said, it will still require quite some elaborated logic to “get” the data for example on save_post() hook (this is when you save a post) from the body.
    This approach would also require you resave each and every post.

    So if you need to update thousands of attachments, you will be looking at even more complex code eventually run inside a page template that gets all posts, then scans all posts, and then updates all found attachments, one by one in a PHP foreach loop.

    There are surely many ways to do this in code, but it would be above the scope of the support here to provide such code, I think.

    Depending on how comfortable you are with Custom Code it will be possible to craft such code using the WP Documentation and eventually scanning forums like Stackoverflow to see examples and adapt them.
    You are of course welcome to ask here on the forum follow up questions as well ??

    I hope this helps.

Viewing 1 replies (of 1 total)
  • The topic ‘Save caption texts as description to the according images (in bulk)?’ is closed to new replies.