JSON Schema Plugin

Just published a little plugin for making structured schema for search engines. No more messing about manually with JSON in your templates :slight_smile:

2 Likes

As someone not too familiar with manually generating JSON, I’d love to learn how to use this plugin to save myself time, but I think I need more guidance on your GH page. When you have time, add in some more examples, if you would. Thanks!

It really depends what you are trying to describe. For example, a simple example is describing a video on the page, which would look like this…

<?= $site->schema('VideoObject')
  ->name('Funny Cat Poses 2.0')
  ->description("A short description of your video, we'd keep it at 140 characters just to be safe.")
  ->thumbnailUrl('http://www.example.com/thumbnail.jpg')
  ->uploadDate('2015-04-05T08:00:00+02:00')
  ->duration('PT1M33S')
  ->contentUrl('http://www.example.com/movie.mp4')
  ->embedUrl('http://www.example.com/embed?videoetc')
  ->author($site->schema('Person')->name('Jason Bourne'))
   ?>

Which would generate:

<script type="application/ld+json">
{
  "@context": "https:\/\/schema.org",
  "@type": "VideoObject",
  "name": "Funny Cat Poses 2.0",
  "description": "A short description of your video, we'd keep it at 140 characters just to be safe.",
  "thumbnailUrl": "http:\/\/www.example.com\/thumbnail.jpg",
  "uploadDate": "2015-04-05T08:00:00+02:00",
  "duration": "PT1M33S",
  "contentUrl": "http:\/\/www.example.com\/movie.mov",
  "embedUrl": "http:\/\/www.example.com\/embed?videoetc",
  "author": {
    "@type": "Person",
    "name": "Jason Bourne"
  }
}
</script>

Of course, you can use PHP / Field values to fill them in.

There are sites like Steal our JSON that offer good starting points, but are not exhustive examples of what is possible. You can also run the page through Google’s Structured data tool to validate it and improve.

I hope that helps.

1 Like

Thank you! This makes more sense already. Cheers! :grin: :kirby:

@luxuryluke

Here is another example if it helps. i just worked out for generate breadcrumb schema using the plugin. If you (or anyone) knows how to shorten this, id love to hear about it :slight_smile:

<?php

$itemListElement = [];

foreach($site->breadcrumb() as $crumb) {

$depth = $site->breadcrumb()->indexOf($crumb) + 1;

$itemListElement[] =
  $site->schema('ListItem')
  ->position($depth)
  ->item(['name' => $crumb->title()->value(), '@id' => $crumb->url()]);
}

echo $site->schema('BreadcrumbList')
->itemListElement($itemListElement);

?>
1 Like