Luc Shelton

SilverStripe: JSON-LD Structured Data

SilverStripe: JSON-LD Structured Data

SilverStripe: JSON-LD Structured Data

SilverStripe: JSON-LD Structured Data

2 Minute(s) to read
Posted 2 years ago Updated 24 months ago 2 Minute(s) to read 0 comments

This website is currently powered by SilverStripe which is an all encompassing Content Management System (CMS) and framework for building web applications using PHP. I chose to adopt this framework some time ago because I had yet to find anything that came close, using the web-development stack that I would otherwise enjoy using (TypeScript etc.).

SilverStripe fortunately comes out of the box with a lot of features (including blogging), but there are some things that are missing that could make it much more competitive when developing websites that are performant with search engine results.

JSON-LD

I've been spending some time trawling through the Google Developer pages trying to find better ways of optimising my website for search engine performance. I've already written a blog post here on best practices for optimising page performance and load times (which can impact search result rankings), but this blog post is more focused on exposing relevant metadata that search engines can use for recognising the type of content that your website has.

In particular, Google provides support for enhancing search results for pages that publishes information such as Recipes, Events, and News Articles.

Extending SilverStripe for JSON-LD Support

After reading through the Google Developer pages, I naturally was inclined to search SilverStripe's module repository to find out whether or not there was any thing already available that suited my needs. While there were some interesting projects that seemed almost relevant, they also still seemed largely incomplete, or only supported an older version of the framework.

Relevant SilverStripe module projects include:

I decided to take it upon myself to write a module extension that would suit my needs, and offer comprehensive support to the entirety of the available and supported schema supported by Google search engine crawlers.

You can install my module with the following command:

#!/bin/bash
composer require loveduckie/silverstripe-json-ld-structured-data

It shouldn't require any other additional configuration, but you will need to add the following code to your parent Page.ss template file.

<html>
<head>

...

$PageStructuredData().RAW

...

</head>
<body>

...

</body>
</html>

This will the invoke this piece of code that is responsible for generating breadcrumbs and invoking any extension functions available for the SilverStripe\CMS\Model\SiteTree type.

<?php

namespace LoveDuckie\SilverStripe\JsonLDStructuredData\Extensions;

use Exception;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Config\Config;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\CMS\Controllers\ContentController;

class JsonLDStructuredDataExtension extends DataExtension
{

...

    public function PageStructuredData()
    {
        $structuredDataContainer = [];
        return $this->InjectedStructuredData($structuredDataContainer);
    }

...

}

You can view the rest of the source code for this module on my GitHub repository here.


Programming Languages:

PHP Bash Shell Script


Comments

Comments