0

I have in the heder.php the following snippet:

<?php
$schema = get_post_meta(get_the_ID(), 'schema', true);
if (!empty($schema)) {
    echo $schema;
}
?>

And in some posts I have the custom field schema with all the <script type="application/ld+json">{... content here

I had this few years and it was always working fine.

Since few days in Google Search Console I see errors Unparsable structured data -> Invalid top level element "null"

I see that the DOM renders the right <script type="application/ld+json"> code for logged in users.

But for non logged in users it renders <script type="application/ld+json">null</script>

Any idea what could have happened and how to resolve it? There are no errors in the console.

Here is an example of a page that has this issue.

Update: I've also tried the following:

$key_name = get_post_custom_values($key = 'schema');
if (!empty($key_name[0])) {
    echo $key_name[0];
}

But there is the same problem. Works for logged in users, and does not work for logged of visitors.

I tried also: <?php echo get_post_meta($post->ID, 'schema', true); ?>, but the same problem.

P.s. in the debug.log there are no errors. Just some deprecation notices. Also no console.log errors.

Update 2: It seems I found something relevant. In the custom field I have a similar code much longer)... but it seems that the null appears if I add the description:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Event",
  "name": "name name",
  "image": "path to url",
  "description": "some text some text - some text: some text",
}
</script>

I fi remove the description it works fine. So I thought it may be related to the : character.... and yes! If I escape \: it works!

It seems something changed in the parser in the Wordpress new versions.... because in the past it was working fine. That json was showing well for years... The same happens for new lines (enter) in strings.

0

Browse other questions tagged or ask your own question.