Skip to content

Copy Post: preserve backslashes in duplicated post content#48376

Closed
shsajalchowdhury wants to merge 1 commit into
Automattic:trunkfrom
shsajalchowdhury:fix/copy-post-backslash-stripping
Closed

Copy Post: preserve backslashes in duplicated post content#48376
shsajalchowdhury wants to merge 1 commit into
Automattic:trunkfrom
shsajalchowdhury:fix/copy-post-backslash-stripping

Conversation

@shsajalchowdhury
Copy link
Copy Markdown
Contributor

What was the problem?

When duplicating a post via the Copy Post feature, backslash characters (\) were stripped from the post content. For example, \t is a tab in a code block became t is a tab.

This happened because wp_update_post() calls wp_unslash() on array input (via wp_insert_post()sanitize_post()), but the source data from get_post() is already unslashed. The double unslashing removes legitimate backslash characters.

Closes #46020

How was it fixed?

Added wp_slash() to the data array before passing it to wp_update_post(). This way, wp_unslash() inside wp_update_post() restores the original content with backslashes intact.

This matches the documented behavior in WordPress core: wp_update_post() only auto-slashes when passed an object, not an array. When passing an array, the caller is responsible for slashing the data.

Reference: https://developer.wordpress.org/reference/functions/wp_update_post/

Changes

  • projects/plugins/jetpack/modules/copy-post.php — wrap $data in wp_slash() before wp_update_post()
  • projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php — added tests for backslash preservation
  • projects/plugins/jetpack/changelog/fix-copy-post-backslash-stripping — changelog entry

Testing instructions

  • Create a post with a Code block containing \t is a tab
  • Save the post
  • From the post list, click "Duplicate" (Jetpack Copy Post)
  • Open the duplicated post
  • Verify the backslash is preserved: \t is a tab (not t is a tab)
  • Also test with content containing \\, \n, \f to confirm all are preserved
  • Run existing tests: jetpack test php --filter=copy-post

Does this pull request change what data or activity we track or use?

No. This PR only fixes how post content is passed to a WordPress core function. No data collection, tracking, or privacy-related changes.

When duplicating a post, backslash characters were being stripped from
the content. This happened because wp_update_post() calls wp_unslash()
on array input (via wp_insert_post -> sanitize_post), but the data
from get_post() is already unslashed.

The fix adds wp_slash() before passing the data array to
wp_update_post(), so wp_unslash() inside restores the original content
with backslashes intact.

Includes tests verifying backslash sequences like \t, \n, \\ are
preserved when copying posts.

Fixes Automattic#46020
@github-actions github-actions Bot added [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests labels Apr 29, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


@github-actions github-actions Bot added the OSS Citizen This Pull Request was opened by an Open Source contributor. label Apr 29, 2026
@tbradsha
Copy link
Copy Markdown
Contributor

Somehow this PR slipped through the cracks and a separate fix was merged (#48870), so I'll close this one, but thanks for your contribution!

@tbradsha tbradsha closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OSS Citizen This Pull Request was opened by an Open Source contributor. [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy post strips "\" characters from post content

2 participants