1

What happens:

  • Write a post
  • Create a gallery
  • Upload a random picture
  • Set caption to "test"
  • Publish Post
  • Go to Media -> Select the picture from before
  • Set caption to "changed" and save
  • Go back to post
  • Check the image
  • Image caption still shows "test" -> Should be "changed"

So if I ever update the media library with proper captions, alt text and titles it looks like they will never be updated on older posts. Is this how it should work?

If this is not a bug, but a feature, how to fix this? Where are these block-information on the post saved?

Kind Regards, Ben

2 Answers 2

1

Yes, that is Core behavior. The Post Editor saves whatever alt text, caption, and title you use while editing a specific Post/Page/CPT into its post content, in HTML if you are using the Classic Editor, or in Image block attributes if you are using the Block Editor.

One workaround is to always upload media directly to the Media Library. While you upload, set your desired alt text, captions, and titles. After you're done there, edit the Posts/Pages/CPTs where you want to add the images. Since the info is stored in the Media Library, when you add the image to a post's content, that will prepopulate from what you already entered.

If you need to go back and change existing image info in posts, it is probably easiest to follow a similar pattern: set the alt text, captions, and titles in the Media Library first. Then, you'll have to search through your Posts/Pages/CPTs, but you can remove the images there and re-add them, and they'll have the info prepopulated.

3
  • 1
    That's really sad to hear. Its quite counter intuitive. Will have to look on how to modify those changed automatically.
    – CasualBen
    Mar 2, 2022 at 10:00
  • I am starting to get very worried about the way wordpress is heading. One of a few major issues, as alt text is important for SEO no matter what wordpress says. Really sad indeed. Aug 24, 2023 at 19:07
  • I get the sense that WP does this so you can adjust the alt text across various pages and choose to include captions some places but exclude them others. But I wouldn't say this is as much to do with the way WP is heading as just the way this has always been. You can always submit a Trac ticket to get more input from the Core developers about whether they would pursue a task to change this behavior.
    – WebElaine
    Aug 28, 2023 at 13:59
1

I've found a solution to this and created a plugin for the current WordPress Version: https://github.com/DasBen/CasualBen-Automatic-Alt-And-Caption-Text/tree/1.1.0

Everytime an image is rendered, there will now be a query to update alt-text and captions.

For those of you who would rather write their own. Here is the code:

add_filter( 'render_block', function( $content, $block ) {
if( 'core/image' !== $block['blockName'] )
    return $content;

    $alt = get_post_meta( $block['attrs']['id'], '_wp_attachment_image_alt', true );
    if( empty( $alt ) ) return $content;

    // Empty alt
    if( false !== strpos( $content, 'alt=""' ) ) {
        $content = str_replace( 'alt=""', 'alt="' . $alt . '"', $content );

    // No alt
    } elseif( false === strpos( $content, 'alt="' ) ) {
        $content = str_replace( 'src="', 'alt="' . $alt . '" src="', $content );
    }

    return $content;
}, 10, 2 );
2
  • 1
    Great work, but such short code could have (and should have) easily been posted here instead of a link, so no +1. Aug 24, 2023 at 19:14
  • 1
    @GavinSimpson that is true. I did not think about it at that time, but here you go :)
    – CasualBen
    Aug 25, 2023 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.