fabianmichael / kirby-meta

All-in-one solution to all of your SEO/OpenGraph/JSON-LD needs. 👀

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to implement hooks?

ImaCrea opened this issue · comments

Hello there,
sorry if this is not a real issue but more a kind ask for help. :)

I understand I can use hooks to prefill metadata with description already of my page but I'm not sure where to put the code you show on the readme.

I'm pretty new with kirby, thanks for your understanding. (I've tried a few ways by modifying config.php as it's said on kirby doc but nothing seems to happen).

cheers and thanks for this great plugin.

Hi @ImaCrea,

hooks belong into your site/config/config.php file, see https://getkirby.com/docs/reference/system/options/hooks.

Hope that helps. :-)

Hello @fabianmichael thanks for your answer.

Yes, I read this and added this to my config file but nothing seems to happen. Here's my config file:

<?php

return [
  'debug' => true,
  'languages' => false,
  'routes' => [
    [
      'pattern' => '(:any)',
      'action'  => function ($uid) {
        $page = page($uid);
        $link = pages('abouts')->children()->first()->url();
        if($page && $page->intendedTemplate() == 'abouts') {
          return go($link, 301);
        }
        return $page;
      }
    ]
  ],
];

return [
  'meta.load:after' => function (
    array $metadata,
    Kirb\Cms\Page $page,
    ?string $languageCode
  ) {
    if (empty($metadata['meta_description']) === true) {
      $metadata['meta_description'] = $page->description();
    }
    return $metadata;
  },
];

The idea is to prefill the description meta tag with the content of the 'description' field of the current page.

I tried also :

<?php

return [
  'debug' => true,
  'languages' => false,
  'routes' => [
    [
      'pattern' => '(:any)',
      'action'  => function ($uid) {
        $page = page($uid);
        $link = pages('abouts')->children()->first()->url();
        if($page && $page->intendedTemplate() == 'abouts') {
          return go($link, 301);
        }
        return $page;
      }
    ]
  ],
  'hooks' => [
    'meta.load:after' => function (
      array $metadata,
      Kirb\Cms\Page $page,
      ?string $languageCode
    ) {
      if (empty($metadata['meta_description']) === true) {
        $metadata['meta_description'] = $page->description();
      }
      return $metadata;
    },
  ]
];

And here I get a typerror : Kirby\Cms\App::{closure}(): Argument #2 ($page) must be of type Kirb\Cms\Page, Kirby\Cms\Page given

I guess this is because meta.load:after is not an event declared by kirby but by the plugin.

And here I'm stuck. This might be quiet easy in the end, sorry for not finding how. Thanks

Hi @ImaCrea,

you need to have a single return statement in your config.php file, because PHP will stop execution after the first return statement. Also, you config contains a typo: Kirb\Cms\Page instead if Kirby\Cms\Page.

thanks, it's working with the second solution and the typo fixed! Awesome :)
you might wanna update the readme then, as the typo comes from there.

@ImaCrea thanks for the hint, fixed the README now.