This WordPress plugin converts image filenames to text that can be added as Caption, Title and Description of the media file. Additionally, the caption can be displayed as <figcaption>
element under images in posts. The plugin is coded in PHP language using WordPress actions and filters.
Stack:
Type:
Project purpose and goal
The purpose of the project was to practice WordPress plugin development using OOP approach learned during the Become a WordPress Developer course.
Back during my photography career, while publishing new posts with images, I had to add meaningful captions, titles and descriptions for each uploaded image manually. This was very time consuming. So, I decided to work on an automated solution. I coded a plugin where a meaningful filename of an image can be used to add all the details (Caption, Title and Description) and the Caption can be displayed under each image.
Overview
The plugin is coded in PHP language using WordPress actions and filters such as admin_init
, admin_menu
, and the_content
. I used the OOP (Object-oriented programming) approach.
The plugin has a basic settings page with some instructions and options checkboxes.
The changes are applied only for the images added in the Posts with Image blocks.
Enabling the “Caption” option will not display the Caption text under the image. The additional option “Add figcaption” has to be enabled for the text to be displayed.
Problems and thought process
My initial intention was only to add the image details as Media Post object properties: post_excerpt
, post_title
and post_content
. However, I discovered that simply adding a value for the Caption (post_excerpt
) will not display its text under the images.
I had to implement a separate function for adding the <figcaption>
element under the images. The function collects <figure>
elements from the post content and then associates the element with the Media Post ID by extracting it from HTML class="wp-image-XX"
. The function inserts the <figcaption>
element for each such image, so the Caption text gets displayed.
Work in progress
I am planning to extend the plugin’s functionality later and add an option to automatically add some EXIF data to the <figcaption>
element that might be useful for photographers.