pedroborges / kirby-meta-tags

⬢ HTML meta tags generator for Kirby. Supports Open Graph and Twitter Cards out of the box.
MIT License
98 stars 11 forks source link

JSON-LD not working with meta-tags.templates #13

Open HashandSalt opened 5 years ago

HashandSalt commented 5 years ago

As the title says. If you set JSON-LD up in meta-tags.default it comes out on every page. If you want a different schema on a specific page (in my case blog post schema), this block also comes out on every page, despite being set within meta-tags.templates for a specific page template.

Other properties in meta-tags.templates work as expected, seems limited to just JSON-LD.

How can I make this work?

pedroborges commented 5 years ago

Could you please share just the JSON-LD settings to reproduce this?

HashandSalt commented 5 years ago

Sure, here you go...


// Meta Tags
'pedroborges.meta-tags.default' => function ($page, $site) {

    $contact = $site->index()->find('hire-a-great-digital-marketing-agency');
    $logo = $site->sitelogo()->toFile() ? $site->sitelogo()->toFile()->url() : '';
    $location = $contact->locmap()->toLocation();

    return [
      'json-ld' => [
          'LocalBusiness' => [
              'name' => $contact->businessname()->value(),

              'url' => $site->url(),
              "email" => $contact->emailaddress()->value(),
              "logo" => $logo,
              "description" => $contact->businessdescription()->value(),
              "sameAs" => [socialprofiles()],
              "telephone" => $contact->telephone()->value(),
              "priceRange" => "£££ - ££££££",
              "openingHours" => [$contact->hours()->value()],
              "address" => [
                "@type" => "PostalAddress",
                "streetAddress" => $contact->streetaddress()->value(),
                "addressLocality" => $contact->addresslocality()->value(),
                "addressRegion" => $contact->addressregion()->value(),
                "postalCode" => $contact->postalcode()->value(),
              ],
              "geo" => [
                "@type" => "GeoCoordinates",
                "latitude" =>  $location->lat()->value(),
                "longitude" => $location->lon()->value(),
              ],
              'image' => $site->find('web-design-client-work')->heroslidesfield()->toFiles()->shuffle()->first()->url(),
          ]
      ],

},

'pedroborges.meta-tags.templates' => function ($page, $site) {

  $logo = $site->sitelogo()->toFile() ? $site->sitelogo()->toFile() : '';

  return [
      'blog' => [

        'json-ld' => [
            'BlogPosting' => [
              'mainEntityOfPage' => [
                '@type' => 'WebPage',
                '@id' => $page->url()
              ],
              'headline' => $page->seotitle()->value(),
              'image' => [
                '@type' => 'ImageObject',
                  'url' => $page->images()->filterBy('feature', true)->first()->focusCrop(1280, 720)->url(),
                  'width' => 1280,
                  'height' => 720

              ],
              'datePublished' => $page->date()->toDate('Y-d-m'),
              'dateModified' => $page->modified('Y-d-m'),

              'author' => [
                '@type' => 'Person',
                  'name' => 'Person Name',

              ],

              'publisher' => [
                '@type' => 'Organization',
                  'name' => 'Hash&Salt',
                  'logo' => [
                    '@type' => 'ImageObject',
                      'url' => $logo->url(),
                      'width' => $logo->width(),
                      'height' => $logo->height()

                    ],

              ],
              'description' => $page->seometa()->value(),
            ]
        ],

    ],

  ];

},
pedroborges commented 5 years ago

I'm not knowledgeable about JSON-LD, I see that it includes both the LocalBusiness and BlogPosting schemas on blog templates. Is that the issue? Should the plugin include only BlogPosting in blog templates?

HashandSalt commented 5 years ago

Yes :)

On every page and BlogPosts, i need the i need the LocalBusiness schema. On BlogPosts ONLY i need BOTH the LocalBusiness and BlogPost Schemas.

The plugin just needs to respect the schemas set in the meta-tags.templates config and not merge them with the default ones. They need to be done only on the template they have been set for.

pedroborges commented 5 years ago

Okay, I asked because it's already working like that on my side. See the image (simplified for testing purposes).

Screen Shot 2019-07-31 at 12 42 32

The plugin is including LocalBusiness on all pages and BlogPosting only in the template it's been defined.

HashandSalt commented 5 years ago

Thats odd. For me, it loaded both on the home page. When I went back to the home page, i got a whoops message because it was looking for things that are not on the home. I just uncommented the code and tried again it seems to work. Both schemas were definetly coming out on the home page, i checked.

If i manage to trigger it again, i will post more detail. Probably some weird edge case, or something that your minimal test code is not triggering.

HashandSalt commented 5 years ago

Ok - although im not getting both JSON blocks on the home page anymore, it is sill reading the config for the templates block on the home page and causing a whoops on an image call. It shouldnt be processing this part of the config unless the template matches.