WordPress Plugin: Filename To Caption

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.


Project purpose and goal


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.





Other projects