-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image caption in Media & Text block #14604
Comments
I think this block was not meant to be used with a captioned image, if you'd like to do so, you can use the columns block and have an image in one column and text in the other. Resizing columns is tracked separately. Thanks. |
I understand. Still think it's a shame to not have at least the option available. I will try the column block. |
Noted @Fturttle thanks for the feedback. I'll keep it in my mind and if we noticed that it's something people are asking for we'll reconsider. |
I'd like to have the option as well. :) |
I would like the option to show image caption as well |
Hi, because of copyright laws in Europe, especially Germany it is necessary to mention the creator e.g. of a picture directly at the picture. So it's not the question if people ask for it, it's necessary by law and would greatly facilitate the work of the webdesigner having it in all Gutenberg image-blocks. And generally - thanks for your great development work! |
I'd like to add my support for this request. I would expect anywhere that an image can be embedded that it would act similarly to the standard image block, which is to say that it would include an optional caption. WordPress core blocks should be as flexible as possible, and also act predictably. If there's an option to add an image, I assume the image interface would be similar to, if not actually, the core image block. Are there several different yet similar image components in use under the hood? |
I need this too. |
Required! Why are you closing this? |
I would like to see captions added to the Media & Text block as well. As stated above, images should have the same options anywhere they are embedded for consistency. |
I'm reopening this issue for potential consideration. It's still unclear to me how this would work... HTML wise it might not make sense because of the nesting... But let's see. |
Another vote for this option. I've got a client that very specifically needs in-page photo credits for every photograph. They've got a bunch of the Media & Text blocks and now I need to figure out a way to get a little text caption underneath. Would love if it worked a bit more like a regular image object. |
I'd like this feature as well :) |
I was unpleasantly informed my our theme developer and QA that this doesn't exist. The solution to use columns is a workaround yes, but it seems to me it pretty much makes this block value questionable altogether. |
I would like the option to show image caption as well |
I would like this option as well. As it was previously mentioned, this feature is highly needed in order to add copyright information to the pictures. |
We are having this very issue right now where we need a caption below an image. We have specific styles applied to the block so when attempting to change the block type, it creates a style issue there. Adding a caption directly below the image would allow a clickable link to open the image in a new tab. |
There are a few things that make mimicking media + text by using columns clunky:
Altogether, this adds up to a very time-consuming process. All this could be avoided by adding captioning to Media + Text blocks. |
Hi folks, you can just filter the output of the media & text block to add an image caption. The following snippet displays the image's caption text which is editable in the Media Library. I haven't tested the following code extensively, but it's working for me in Wordpress version 5.5.1. Add this code to your theme's functions.php file: //filter Media & Text block output to add image caption
function media_block_caption( $block_content, $block ) {
if ( $block['blockName'] === 'core/media-text' ) {
$mediaId = $block['attrs']['mediaId'];
if($mediaId){
$image = get_post($mediaId);
$image_caption = $image->post_excerpt;
if($image_caption){
$content = str_replace('</figure>', '<figcaption>' . $image_caption . '</figcaption></figure>', $block_content);
return $content;
}
}
}
return $block_content;
}
add_filter( 'render_block', 'media_block_caption', 10, 2 ); |
Hi Samrhein |
Thank you samrhein! It works. |
this is amazing, thank you! do you know if theres a way to do this for image titles as well? |
Thanks a lot @samrhein your hook works well. |
Thanks @samrhein I have tried this using wordpress 5.7 and it worked fine. Is there a way to format and position the text below the image. None of the css code solutions that I have added to my child theme for centering caption have worked. |
Following the above I have pasted this code into my child theme css file and it seems to be working ok. I can manipulate fonts alignment etc. |
Same here. The default block should have this option to enable or disable the image caption or maybe a second block for this. And it should have the Lightbox options, too. (but i think this is not needed). @cr0ybot has said it:
|
I actually tried something like this.
|
Wow, four years later and still not implemented! |
Just to add a further voice to the request for this to be implemented. It is an expected feature of all images. |
I needed this too. :) |
I would also like this functionality, thanks 🙏 |
Another request for image captions in the Media+Text block. @samrhein 's solution is working for me in WP 6.4.1 |
Thank you @samrhein This worked perfect!! My client is in Canada, and we needed this for legal reasons. It is all a good way to keep people on site longer, as well as a way to give credit where needed. |
Hi all, This all worked great, however, I still had significant padding above the caption. Nothing worked. Finally, we added code for the image, which worked! Here is our code for anyone who needs it: figcaption{ img{ |
I need also this functionality. Thanks. I tried the above suggested code on WP 6.4.2. : the code of @samrhein is doing fine, even taking into account the html code (href) in the caption on my image. The code of @ajmaurya99 is also doing fine, but doesn't process the html code, so in my caption The only annoying thing is that I have also an audio block in this media&text block. Now this audio block also gets the caption of the image, as this audio block is also a By the way, it would also be nice to have the possibility to add a caption on an audio block: for the same reason: it must be possible to indicate a copyright on the audio. Thanks. I searched a bit to cope with the fact that the code of @samrhein adds the caption to every //filter Media & Text block output to add image caption, only on the media image
function media_block_caption( $block_content, $block ) {
if ( $block['blockName'] === 'core/media-text' ) {
$mediaId = $block['attrs']['mediaId'];
if($mediaId){
$image = get_post($mediaId);
$image_caption = $image->post_excerpt;
if($image_caption){
$lrpos=strpos($block_content,'has-media-on-the-right'); /* check if the media is left or right */
if ($lrpos == false) {
$w=strpos($block_content,'</figure>'); /* left: insert caption on first <figure> tag */
} else {
$w=strrpos($block_content,'</figure>'); /* right: insert caption on last <figure> tag */
}
if ($w !== false){
$content = substr($block_content,0,$w) . '<figcaption>' . $image_caption . '</figcaption>' . substr($block_content,$w);
return $content;
}
}
}
}
return $block_content;
}
add_filter( 'render_block', 'media_block_caption', 10, 2 ); Hopes this could help some of you. |
Almost all of my clients still use the Classic Editor. It is difficult for us to understand why such decisions have been made with the Block Editor. Copyright is important for professional websites. The image block can do it too and it is also editable in the editor. I see no reason why every media element, regardless of type, should not have the option of adding a caption. |
I have not been able to find a way to add an image caption in the ‘Media & Text’ block.
tag manually it returns an “HTML Error” and messes up everything.I looked at it through the code editor and there’s no
<figcaption></figcaption>
tags.Yes, my image has a caption set in the media library.
If I add
Can anyone explain this?
The text was updated successfully, but these errors were encountered: