Skip to content

Image Block: Preserve line breaks in media caption #70476

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

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

BugReportOnWeb
Copy link
Contributor

What?

Closes #59911

This PR ensures that newline characters (\n) in image captions, particularly when added via the Media Library are preserved and rendered correctly in both the editor and the frontend.

Why?

When an image caption is populated from the Media Library, it may include newline characters (\n). These appear as line breaks initially in the editor but are lost after a refresh, as \n gets converted to spaces. As a result, line breaks are not respected unless manually re-entered in the editor. confusing for users.

How?

Added a useEffect in the ImageEdit component to detect when the caption is a string containing \n, and replace those newline characters with <br /> before the data is passed to Caption component. This ensures consistent rendering by using proper HTML line breaks.

Testing Instructions

  1. Upload an image through Media Library and include a multi line caption (using Enter or Shift+Enter).
  2. Insert the image into a post using the Image block.
  3. Verify that the line breaks are respected in the editor.
  4. Refresh the editor page.
  5. Verify that the caption still retains line breaks in the editor.
  6. Save the post and view it on the frontend.
  7. Confirm that line breaks are correctly rendered on the frontend as well.

Screenshots or screencast

Before

Screen.Recording.2025-06-19.at.12.35.01.AM.1.mov

After

Screen.Recording.2025-06-19.at.7.25.16.PM.mov

@BugReportOnWeb BugReportOnWeb marked this pull request as ready for review June 19, 2025 14:47
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @JulienGardair.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: JulienGardair.

Co-authored-by: BugReportOnWeb <[email protected]>
Co-authored-by: tellthemachines <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@shail-mehta shail-mehta added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block labels Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Image Affects the Image Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Media Caption Line Break in media library
2 participants