Can’t print Yoast meta description into page template (syntax error, unexpected ‘.’)

The question:

Here is the relevant portion of my page template code:

$yoast_meta = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);

function my_service_template_footer_data() {
    echo "<script type='application/ld+json'>
{
    '@context': 'https://schema.org/',
    '@type': 'Service',
    'serviceType': ' " . get_the_title() . " ',
    'description': ' " . $yoast_meta; . " ',
    'provider': {
      '@type': 'LocalBusiness',
      'name': '...',
  'address': {
      '@type': 'PostalAddress',
      'addressLocality': '...',
      'addressRegion': '...',
      'postalCode': '...',
      'streetAddress': '...'
    },
    'providerMobility':'...',
    'telephone': '...',
    'image': '...',
    'PriceRange': '...',
    'serviceArea': [{
        '@type': '...',
      'name': '...',
      '@id': '...'
      }]
  }
}
</script>";
        
}

When I save the file I get this error from WP_DEBUG:

Parse error: syntax error, unexpected ‘.’ in /home/260255.cloudwaysapps.com/hyubphmutk/public_html/wp-content/themes/twentytwentyone-child/functions.php on line 99
There has been a critical error on this website.

How can I re-use the Yoast meta description? Thanks!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

There is indeed an unwanted ; (semicolon) in your function (try to look at line 99 in the twentytwentyone-child/functions.php file) which likely is the cause to the syntax error. So just remove that ; and the error would be gone:

function my_service_template_footer_data() {
    echo "<script type='application/ld+json'>
{
    '@context': 'https://schema.org/',
    '@type': 'Service',
    'serviceType': ' " . get_the_title() . " ',
    'description': ' " . $yoast_meta; . " ', // remove the ;
    ...

But that variable is not actually defined or that it is out of scope in my_service_template_footer_data(), so you should move/copy the variable into that function, and use get_the_ID() instead of $post->ID, like so:

function my_service_template_footer_data() {
    $yoast_meta = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
    ...

However, your JSON data is not in the valid format, e.g. property names should be wrapped in double quotes, so for example you would use "description" instead of 'description'. Also, you must properly escape the property values which can be done via the wp_json_encode() function (an enhanced version of the native json_encode() function in PHP).

So having said that, try this which uses the ?>HTML here<?php syntax and not echo 'HTML here':

Update: The above JSON-encoding functions will enclose the return value in double quotes, so don’t do "<?php echo wp_json_encode( ... ); ?>", i.e. do not include the quotes (").

function my_service_template_footer_data() {
    $yoast_meta = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
    ?>
<script type="application/ld+json">
{
    "@context": "https://schema.org/",
    "@type": "Service",
    "serviceType": <?php echo wp_json_encode( get_the_title() ); ?>,
    "description": <?php echo wp_json_encode( $yoast_meta ); ?>,
    "provider": {
        "@type": "LocalBusiness",
        "name": "...",
        "address": {
            "@type": "PostalAddress",
            "addressLocality": "...",
            "addressRegion": "...",
            "postalCode": "...",
            "streetAddress": "..."
        },
        "providerMobility":"...",
        "telephone": "...",
        "image": "...",
        "PriceRange": "...",
        "serviceArea": [{
            "@type": "...",
            "name": "...",
            "@id": "..."
        }]
    }
}
</script>
    <?php
}

PS: If get_the_ID() isn’t giving you the correct ID, try using get_queried_object_id() instead, and then use get_the_title( get_queried_object() ). ( And you can validate your structured data here.. 🙂 )


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment