Introduction
Structured data (schema.org JSON-LD) for search engines
Fegg\Tool\JsonLd builds schema.org structured data: the site's name and organization, breadcrumbs and articles, which search engines can show in their results. The methods return arrays; the template writes them as JSON in a <script type="application/ld+json"> element.
How to use
The methods are static. A controller sets the data as the site information json_ld with setSiteinfo, usually several items in one graph():
use Fegg\Tool\JsonLd;
public function detail($id = '')
{
// ... $this->page['data'] is the post
$data = $this->page['data'];
$siteName = $this->config['site_info']['name'];
$this->setSiteinfo('json_ld', JsonLd::graph(
JsonLd::article(array(
'headline' => $data['title'],
'datePublished' => $data['post_date'],
'dateModified' => $data['modified'],
// getImageUrl() includes the base path: only the domain is added
'image' => $data['image_id'] ? FEGG_CURRENT_DOMAIN . getImageUrl($data['image_id']) : '',
'url' => '/news/post_' . $data['news_id'] . '.html',
'author' => $siteName,
'publisher' => $siteName,
)),
JsonLd::breadcrumb(array(
'ホーム' => '/',
'ニュース' => '/news/',
$data['title'] => '',
))
));
$this->displayPage('news/detail');
}
The skeleton's siteframe writes it in the <head> of every page that sets it:
{{ if $site.json_ld }}
<script type="application/ld+json">{{ $site.json_ld|feggJs|noescape }}</script>
{{ end }}
feggJs writes the array as JSON and encodes <, > and &, so a title containing </script> cannot end the element. Use exactly this line: noescape is required inside <script>, and the normal escaping would break the JSON.
In the skeleton, the top page (Index.php) sets the site and its organization, and the news detail page sets the article and its breadcrumbs.
The data
- Empty values (
null,'',array()) are left out, so optional fields can be passed as they come from the database. - Paths (
url,item,image,logo) become absolute URLs withfeggAbsoluteUrl(): a path of the site without the base path (see Absolute URLs). - Dates (
'Y-m-d H:i:s'from the database, a Unix time or a date object) are written in ISO 8601 with the time zone:2026-11-15T10:00:00+09:00.
{
"@context": "https://schema.org",
"@graph": [
{
"@type": "NewsArticle",
"headline": "New shop",
"datePublished": "2026-11-15T10:00:00+09:00",
"url": "https://www.example.com/news/post_12.html",
"author": { "@type": "Organization", "name": "Example Inc." },
"publisher": { "@type": "Organization", "name": "Example Inc." }
},
{
"@type": "BreadcrumbList",
"itemListElement": [
{ "@type": "ListItem", "position": 1, "name": "ホーム", "item": "https://www.example.com/" },
{ "@type": "ListItem", "position": 2, "name": "ニュース", "item": "https://www.example.com/news/" },
{ "@type": "ListItem", "position": 3, "name": "New shop" }
]
}
]
}
Check a page with Google's Rich Results Test after changing its data.
Errors
- A date that cannot be read throws
\InvalidArgumentException.
Methods
JsonLd::graph
Put several items in one script
JsonLd::organization
The company or organization of the site
JsonLd::website
The site itself
JsonLd::breadcrumb
Breadcrumbs of a page
JsonLd::article
An article: a news post, a blog post