Skip to content

Backslashes are stripped from postMeta fields on save (storage-sync.php passes an unslashed value to update_post_meta) #200

Closed

Description

@WebUppAdmin

A field with postMeta storage loses every backslash when the post is saved. The value a\b is stored as ab.

Affected versions. I ran the steps below on 7.7.11 and 7.7.12 and both lose the backslash. I also saw it on 7.7.7. The line is the same in all three.

Steps to reproduce

  1. Register a block with one text field stored in post meta. I put it in a theme folder, blockstudio/note/block.json, with a one line index.php beside it:
{
  "name": "example/note",
  "title": "Note",
  "blockstudio": {
    "attributes": [
      {
        "id": "summary",
        "type": "text",
        "storage": { "type": "postMeta", "postMetaKey": "note_summary" }
      }
    ]
  }
}
  1. Save a post that contains the block with the value a\b. A REST save does it. In the post content the value sits under blockstudio.attributes, like this:
<!-- wp:example/note {"blockstudio":{"attributes":{"summary":"a\\b"}}} /-->
  1. Read the meta: get_post_meta( $post_id, 'note_summary', true ).

Expected. a\b

Actual. ab

Saving again gives the same result. The block attribute in post_content still has the backslash. Only the meta copy loses it.

Other values, same on 7.7.11 and 7.7.12:

Value in the block Value in the meta
a\b ab
a\\b (two backslashes) a\b
q"x\y"z q"xy"z
abc abc

So each backslash is removed, or each pair becomes one. Quotes survive.

Cause. Storage_Sync::sync_field_value() calls update_post_meta( $post_id, $key, $value ) with the value exactly as parsed from the block. WordPress expects meta values to be slashed on input, and update_metadata() runs wp_unslash() on the value before it writes. So a value that was never slashed loses its backslashes.

  • 7.7.7: includes/classes/storage-sync.php line 324
  • 7.7.11: includes/classes/storage-sync.php line 320
  • 7.7.12: includes/classes/storage-sync.php line 320

WordPress core: update_metadata() in wp-includes/meta.php calls wp_unslash( $meta_value ) at line 222 (trunk, read 2026-11-04). The docblock says "For historical reasons both the meta key and the meta value are expected to be slashed".

Fix. One line. I tested it on 7.7.12:

update_post_meta( $post_id, $key, wp_slash( $value ) );

With it, a\b, a\\b and q"x\y"z are all stored exactly as sent, on the first save and on a second save. wp_slash() handles arrays too, so repeater rows should be covered (not run). The update_option() call next to it needs no change (inferred from core, not run).

Other update_post_meta() calls in includes/ may have the same problem. I only checked the one in storage sync.

I searched the open and closed issues and pull requests for "backslash", "slash", "wp_slash" and "unslash". Nothing matches this bug.

Activity

  1. added
    bug
    on Nov 6, 2026
  2. added a commit that references this issue on Nov 7, 2026
    53bb4f6
  3. dnnsjsk commented on Nov 7, 2026

    @dnnsjsk
    Contributor

    Fixed and released in 7.7.13, included in 12440a6.

    Post-meta storage now preserves literal backslashes, quotes, and nested repeater values on initial saves and resaves. Related meta-backed and custom-post-type database writes are protected too.

    After upgrading, re-save affected posts whose saved block attributes still contain the original values to refresh their meta copies.

    Regression coverage includes actual WordPress save hooks, repeaters, normal and nested editor saves, reloads, and unchanged-field resaves. Full CI and distribution checks passed. Thanks for the detailed reproduction and regression tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions