If you’ve been looking at Google Search Console or the Rich Results Test and pulling your hair out over the error “Unparsable structured data > Bad escape sequence in string”, this post is for you.
It’s a frustrating error because your website looks fine to humans, but to Googlebot, your Schema markup is broken, invalid.
Today, I’ll show you exactly why this happens and give you the ultimate, fail-safe method to fix it in WordPress and PHP. Let’s get your structured data valid again.
Video Tutorial: How-to Fix Unparsable Structured Data: Bad Escape Sequence in String
What Does “Bad Escape Sequence” Mean?
To understand the fix, we need to understand the problem. Schema markup is usually written in a format called JSON-LD.
In JSON, the backslash character (\) is a special “escape” character. It tells the browser, “Hey, the very next character means something special.”
For example, in PHP or JSON:
- \n creates a new line.
- \” allows you to put a double quote inside a string that is already wrapped in double quotes.
A “Bad escape sequence” error occurs when you use a backslash followed by a character that JSON doesn’t recognize as special.
If Google sees \Text, it panics because \T isn’t a standard command in that context. It breaks the whole block of code.
4 Most Common Causes of this Error
In my experience helping websites fix their SEO issues, this error almost always comes down to one of these four scenarios when coding manually.
1. The “Single Quote” Trap (Very Common in WordPress)
This is the number one culprit. In JSON, strings must be wrapped in double quotes (“).
Because the string uses double quotes, you do not need to escape single quotes (apostrophes) inside it.
The mistake happens when developers use PHP functions designed for JavaScript, like esc_js(), on data destined for JSON.
The Mistake:
// PHP turning "RankYa's Plugin" into "RankYa\'s Plugin"
$bad_title = esc_js( "RankYa's Plugin" );
// The resulting broken JSON output:
// "headline": "RankYa\'s Plugin"
// escape sequence ERROR in string because JSON doesn't know what \' means.
2. Windows File Paths
If your plugin or theme deals with file paths on a Windows server, you might run into this issue. Windows uses backslashes for folders.
The Mistake:
"path": "C:\Users\RankYa\Documents"
// ERROR! JSON sees \U and \R or \D and throws an error.
The Fix: You must double-escape backslashes so they are treated as literal text characters.
"path": "C:\\Users\\RankYa\\Documents"
3. ISO 8601 Date Formats
Google loves dates in ISO 8601 format (e.g., 2025-03-10T15:30:00Z). In fact, for Google Search Console Structured Data to be valid, date formats must be correctly outputted. In PHP, we use the gmdate function to generate this. We have to use backslashes in PHP to tell it to print the literal letter ‘T’ and ‘Z’.
The Mistake:
// This looks okay in PHP, but can cause issues:
$date = gmdate( 'Y-m-d\TH:i:s\Z' );
Depending on your server setup, that single backslash might get passed to the JSON, causing a \T error. The safest bet in PHP is to double backslash them.
// The safe way:
$date = gmdate( 'Y-m-d\\TH:i:s\\Z' );
4. Regular Expressions (Regex)
If you are developing a complex plugin that saves search patterns or regex rules into a JSON configuration file, you need to be careful.
The Mistake: trying to save a pattern that matches digits (\d+).
"pattern": "\d+"
// ERROR! \d is not a valid JSON escape.
You need to escape the backslash itself: “pattern”: “\\d+”.
The Smart Fix = Stop Manual Concatenation
If you take one thing away from this post, let it be this:
Stop trying to manually build JSON strings by pasting text together.
Trying to build Schema by doing the code below is asking for trouble. You will constantly battle escaping issues hence errors with unparsable structured data in Search Console.
$json = '{ "title": "' . $title . '" }';
The Professional WordPress Solution
The fix is incredibly simple. Stop building strings. Build a PHP Array instead.
PHP has a built-in function called json_encode() (and WordPress has an even better version called wp_json_encode()). These functions handle 100% of the escaping logic for you, automatically.
Here is the “before and after” of how to fix code that is throwing these errors.
The WRONG Way (Manual String Building)
<?php
// This method is prone to "Bad escape sequence" errors
$title = esc_js("RankYa's Video Tutorial"); // esc_js is WRONG for JSON
$schema_output = '<script type="application/ld+json">{
"@context": "https://schema.org",
"@type": "VideoObject",
"name": "' . $title . '", //stiching STRINGS is prone to errors
"uploadDate": "' . gmdate( 'Y-m-d\TH:i:s\Z' ) . '"
}</script>';
return $schema_output;
?>
The RIGHT Way (Using Arrays and wp_json_encode)
This code is cleaner, easier to read, and guarantees you won’t get escaping errors.
<?php
// 1. Prepare your data naturally
$title = "RankYa's Video Tutorial"; // No esc_js needed!
// 2. Build a PHP array representing your structure
$schema_data = array(
'@context' => 'https://schema.org',
'@type' => 'VideoObject',
// wp_json_encode handles the apostrophe automatically
'name' => $title,
// Safe date formatting
'uploadDate' => gmdate( 'Y-m-d\\TH:i:s\\Z' )
);
// 3. Let WordPress turn the array into error free JSON format
$json_output = '<script type="application/ld+json">' . wp_json_encode( $schema_data ) . '</script>';
return $json_output;
?>
Summary
Don’t fight with backslashes. If you are coding Schema markup for WordPress, use PHP arrays to organize your data, and then use json_encode or wp_json_encode() (for WordPress) at the very end. It’s the professional, robust way to ensure Google Search Console stays happy with your structured data.