PHP
Joomla! 6
Joomla! 5
Joomla! 4
Template Override
Intermediate
Inject Structured Schema.org JSON-LD into <head> via HtmlDocument
Outputs valid Article, FAQPage or SoftwareSourceCode structured data in application/ld+json for Google Rich Results.
templates/seu_template/html/com_content/article/default.php
<?php
declare(strict_types=1);
use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
$doc = Factory::getApplication()->getDocument();
$schema = [
'@context' => 'https://schema.org',
'@type' => 'TechArticle',
'headline' => (string) $this->item->title,
'datePublished' => (string) $this->item->publish_up,
'dateModified' => (string) $this->item->modified,
'mainEntityOfPage' => Uri::getInstance()->toString(),
];
$doc->addScriptDeclaration(
json_encode($schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG),
'application/ld+json'
);
Include JSON_HEX_TAG so embedded text can never prematurely close the script tag.
Technical explanation and best practices
Calling $doc->addScriptDeclaration($json, 'application/ld+json') places clean, properly encoded structured data inside the Joomla document head.