0

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!

1 Answer 1

3

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.. :) )

7
  • Thanks Sally! I will give this a try. I abandoned this approach to use something less desirable, but I will see if I can keep going now on this track. =)
    – Drewdavid
    Commented May 15, 2021 at 1:18
  • Sure, take your time 🙂
    – Sally CJ
    Commented May 15, 2021 at 2:39
  • I will check wp_json_encode - I have been starting to try out json_encode but didn't realize there was a wp_json_encode ... hah!
    – Drewdavid
    Commented May 16, 2021 at 2:05
  • 1
    Figured it out - the wp_json_encode() seems to have been adding its own quotes, in addition to the ones in the JSON. I removed wp_json_encode() and got just one set of quotes. :)
    – Drewdavid
    Commented May 17, 2021 at 21:49
  • 1
    Ah yes, sorry my bad! But please don't remove it - see the "Update" in my answer. :)
    – Sally CJ
    Commented May 18, 2021 at 0:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.