MyBB Community Forums

Full Version: Google Structured Data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've added Google Structured Data to my forums thread pages successfully using the following code in the <head> code of the showthread template

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "BlogPosting",
  "mainEntityOfPage": {
    "@type": "WebPage"
  },
  "headline": "{$thread['subject']} - Page {$page}",
  "datePublished": "<?=my_date($mybb->settings['dateformat'], $thread['dateline'])?>",
  "dateModified": "",
  "image": {
    "@type": "ImageObject",
    "height": "REPLACE_ME",
    "width": "REPLACE_ME",
    "url": "REPLACE_ME"
  },
  "author": "{$thread['username']}",
  "publisher": {
    "@type": "Organization",
    "logo": {
      "@type": "ImageObject",
      "url": "REPLACE_ME"
    },
"name":"REPLACE_ME",
"sameAs":[
  "https://twitter.com/REPLACE_ME",
  "https://www.youtube.com/channel/REPLACE_ME"
],
"url":"REPLACE_ME"
  },
  "articleBody": ""
}
</script>

However, I need to add the dateModified property as well, and have got it to work for the datePublished using:

"datePublished": "<?=my_date($mybb->settings['dateformat'], $thread['dateline'])?>",

But I can't work out how to modify the above datePublished code for dateModified

Can anyone help? 


I've solved it. dateModified needs to be changed to the following:

"dateModified": "<?=my_date($mybb->settings['dateformat'], $thread['lastpost'])?>",


So the full code to use for Google Structured Data in the showthread template is:

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "BlogPosting",
  "mainEntityOfPage": {
    "@type": "WebPage"
  },
  "headline": "{$thread['subject']} - Page {$page}",
  "datePublished": "<?=my_date($mybb->settings['dateformat'], $thread['dateline'])?>",
  "dateModified": "<?=my_date($mybb->settings['dateformat'], $thread['lastpost'])?>",
  "image": {
    "@type": "ImageObject",
    "height": "REPLACE_ME",
    "width": "REPLACE_ME",
    "url": "REPLACE_ME"
  },
  "author": "{$thread['username']}",
  "publisher": {
    "@type": "Organization",
    "logo": {
      "@type": "ImageObject",
      "url": "REPLACE_ME"
    },
"name":"REPLACE_ME",
"sameAs":[
  "https://twitter.com/REPLACE_ME",
  "https://www.youtube.com/channel/REPLACE_ME"
],
"url":"REPLACE_ME"
  },
  "articleBody": ""
}
</script>

The next issue, is that the dateModified now shows correctly for older posts, but for new posts it shows the word Today, which is not valid for Google Structured Data.

So can someone help me to change it so it only shows the date always?
It is depending on your date setting in ACP.
Your date is currently set relative and the function my_date() will convert the date format to "Today".

Either you reset your date format in ACP (globally affect) or you replace my_date() function.

Check my_date() function here:
https://crossreference.mybb.de/nav.html?....html#l323

[ExiTuS]
(2020-02-09, 12:43 AM)[ExiTuS] Wrote: [ -> ]It is depending on your date setting in ACP.
Your date is currently set relative and the function my_date() will convert the date format to "Today".

Either you reset your date format in ACP (globally affect) or you replace my_date() function.

Check my_date() function here:
https://crossreference.mybb.de/nav.html?....html#l323

[ExiTuS]

Thanks ExiTus, you set me on the right path.

I needed to add the following to the end of the dateModified line:

, '', false


Which looks like:

"dateModified": "<?=my_date($mybb->settings['dateformat'], $thread['lastpost'], '', false)?>",


Here's the full working Google Structured Data that goes in the showthread template. You just need to fill in the REPLACE_ME bits of the code to use on another site:

<script type="application/ld+json">
{
  "@context": "http://schema.org/",
  "@type": "BlogPosting",
  "mainEntityOfPage": {
    "@type": "WebPage"
  },
  "headline": "{$thread['subject']} - Page {$page}",
  "datePublished": "<?=my_date($mybb->settings['dateformat'], $thread['dateline'])?>",
  "dateModified": "<?=my_date($mybb->settings['dateformat'], $thread['lastpost'], '', false)?>",
  "image": {
    "@type": "ImageObject",
    "height": "REPLACE_ME",
    "width": "REPLACE_ME",
    "url": "REPLACE_ME"
  },
  "author": "{$thread['username']}",
  "publisher": {
    "@type": "Organization",
    "logo": {
      "@type": "ImageObject",
      "url": "REPLACE_ME"
    },
"name":"REPLACE_ME",
"sameAs":[
  "https://twitter.com/REPLACE_ME",
  "https://www.youtube.com/channel/REPLACE_ME"
],
"url":"REPLACE_ME"
  },
  "articleBody": ""
}
</script>
only in showthread template ?

i want to add this code to my forum
(2020-02-09, 02:50 AM)ziuma Wrote: [ -> ]only in showthread template ?

i want to add this code to my forum

The code above is for the BlogPosting @type of google structured data, so is for the thread pages. But you'll want to add more @types throughout the forum and a global organization one too, which could possibly go in the headerinclude, for example:

<script type="application/ld+json">
{
"@context":"http://schema.org",
"@id":"https://www.EXAMPLE_URL.com/#identity",
"@type":"Organization",
"description":"REPLACE_ME",
"image":{
  "@type":"ImageObject",
  "height":"REPLACE_ME",
  "width":"REPLACE_ME",
  "url":"REPLACE_ME"
},
"logo":{
  "@type":"ImageObject",
  "url":"REPLACE_ME"
},
"name":"REPLACE_ME",
"sameAs":[
  "https://twitter.com/REPLACE_ME",
  "https://www.youtube.com/channel/REPLACE_ME"
],
"url":"REPLACE_ME/"
}
</script>