Search code examples
phpwordpressxpathschema

How to Dynamically Populate Schema in WordPress Using XPath Variables?


I have Schema for post type "company" in my WordPress project, but I also have XPath variables but I don't know what to do with them. Can anyone who maybe have worked in XPath help me integrate it with schema?

The XPath Variables:

xpath:movingcompany.name:body/main[1]/div[1]/div[1]/h1[1]
xpath:movingcompany.address:body/main[1]/div[2]/div[1]/div[1]/div[1]/p[2]/span[1]
xpath:movingcompany.image:body/main[1]/div[2]/div[1]/div[1]/div[2]/img[1]
xpath:movingcompany.aggregateRating.ratingValue:body/main[1]/div[1]/div[1]/div[1]/div[1]/span[1]/span[1]
xpath:movingcompany.aggregateRating.ratingCount:body/main[1]/div[1]/div[1]/div[1]/div[1]/span[1]/span[2]
xpath:movingcompany.aggregateRating.reviewCount:body/main[1]/div[1]/div[1]/div[1]/div[1]/span[1]/span[2]
xpath:movingcompany.openingHoursSpecification.opens:body/main[1]/div[9]/div[1]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/div[1]/span[1]
xpath:movingcompany.openingHoursSpecification.closes:body/main[1]/div[9]/div[1]/div[1]/div[1]/table[1]/tbody[1]/tr[1]/td[2]/div[1]/span[1]

The Schema:

{
  "@context": "https://schema.org/",
  "@type": "MovingCompany",
  "name": "{{movingcompany.name}}",
  "address": "{{movingcompany.address}}",
  "image": "{{movingcompany.image}}",
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": "{{movingcompany.aggregateRating.ratingValue}}",
    "ratingCount": "{{movingcompany.aggregateRating.ratingCount}}",
    "reviewCount": "{{movingcompany.aggregateRating.reviewCount}}"
  },
  "openingHoursSpecification": {
    "@type": "OpeningHoursSpecification",
    "opens": "{{movingcompany.openingHoursSpecification.opens}}",
    "closes": "{{movingcompany.openingHoursSpecification.closes}}",
    "dayOfWeek": [
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday"
    ]
  }
}

I tried discovering how to configure XPath Variables and integrate them with Schema, but I couldn't find a valid solution that worked.

Thanks beforehand for a lend of a hand.


Solution

  • I used Google Tag Manager and it helped me.

    Full guideline.

    I appreciate everyone support on this issue. Thanks.