Issue with adding structured data as json format to Joomla using PHP.
When I add a script declaration of type = json it will be combined in the header.
$jsonscript = array();
$jsonscript['website'] = '
{
"@context": "http://schema.org/",
"@type": "WebSite",
"name": "ECMA"
"url": "https://example.com/"
}';
$jsonscript['organization'] = '
{
"@context": "http://schema.org",
"@type": "Organization",
"url": "https://example.com/",
"logo": "https://example.com/images/logo-example.png",
"contactPoint": [
{
"@type": "ContactPoint",
"telephone": "+31-6-11",
"contactType": "customer service",
"availableLanguage": ["Dutch", "English"]
}
],
"name": "ECMA"
}
';
foreach ($jsonscript as $content) :
$document->addScriptDeclaration($content, $type = 'application/ld+json');
endforeach;
This will be rendered:
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "WebSite",
"name": "ECMA",
"url": "https://www.example.com/"
}
{
"@context": "http://schema.org",
"@type": "Organization",
"url": "https://www.example.com",
"logo": "https://example.com/images/logo-example.png",
"contactPoint": [
{
"@type": "ContactPoint",
"telephone": "+31-6-11",
"contactType": "customer service",
"availableLanguage": ["Dutch", "English"]
}
],
"name": "ECMA"
}
</script>
Th rich snippets testing tool does not recognize the second json part. It will only recognize type = website.
It would be great if some kind of parameter can be added which will prevent merging the tags.
The output I would like to see is:
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "WebSite",
"name": "ECMA",
"url": "https://www.example.com/"
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"url": "https://www.example.com",
"logo": "https://example.com/images/logo-example.png",
"contactPoint": [
{
"@type": "ContactPoint",
"telephone": "+31-6-11",
"contactType": "customer service",
"availableLanguage": ["Dutch", "English"]
}
],
"name": "ECMA"
}
</script>
Comma separated entries seems not to work for the Rich Snippets Testing Tool
When this feature request is granted you are able to add json to the head of your website using the function addScriptDeclaration.
Labels |
Added:
?
|
Category | ⇒ | JavaScript |
Ah, sorry, now I understand your feature request.
I'm using ->addCustomTag(...)
in foreach
instead of ->addScriptDeclaration(...)
foreach (severalSnippetsCollections as $collected_data_array)
{
$output = array('<script type="application/ld+json">');
$output[] = json_encode($collected_data_array);
$output[] = '</script>';
JFactory::getDocument()->addCustomTag($output);
}
Status | New | ⇒ | Discussion |
@ReLater using the function addCustomTag
can be used too, but the function addScriptDeclaration
seems to be made for the job.
joomla-cms/libraries/src/Document/Document.php
Lines 571 to 593 in 461eb0d
I'm trying to add a script
with $type="application/ld+json"
to the head
of my Joomla site.
The only missing part of this function is the ability to add the script separated from other scripts with the same type.
Labels |
Added:
J3 Issue
|
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2019-08-05 08:40:56 |
Closed_By | ⇒ | franz-wohlkoenig |
closed as stated above.
Hm, I think you're doing something wrong by building your JSON strings manually instead of using json_encode($some_data_collection).
Why don't you collect your rich snippets datas in a (maybe multidimensional) $array first and in the end run a single json_encode($array); to get the correct JSON string.
$document->addScriptDeclaration(json_encode($array), $type = 'application/ld+json');