0

I am trying to modify the caption of images in posts by filtering img_caption_shortcode, and then displaying the content using the_content(). I want to add bylines/credit on images, so I have created two fields in ACF that should be added inside the <figcaption> on all my posts. Basically I want to do what is description in the blog.

So from this:

<figcaption> So this is a caption that describes the image. </figcaption> 

To this:

<figcaption class="post-thumbnail-caption"> So this is a caption that describes the image. <span class="wp-block-image-byline">Photo: Gregor Gregson</span> </figcaption> 

I have tried just doing it as simple as possible, but I can not get it to work. Nothing happans/changes with the following code in my functions.php (I did first try the whole code from the example in the blog linked above).

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 ); function my_img_caption_shortcode( $output, $attr, $content ) { $output = 'test'; return $output; } 

Any ideas on what I could be doing wrong?

2
  • Images can be added in many ways (manually via custom HTML, using the Image block in Gutenberg, added by plugins, etc.), so the hook img_caption_shortcode doesn't always get called, hence you'd need to find another way to add the custom field to the caption, e.g. this for the post thumbnail caption. Commented Feb 10, 2021 at 5:01
  • 1
    @SallyCJ as I myself have found out, yes shortcodes are not called in Gutenberg as it uses Blocks (unless you manually add shortcodes). So one way seems to be filtering render_block, and/or editing the standard image block to behave the way I want. So a bit more complicated than the above method. Commented Feb 13, 2021 at 10:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.