Setting URL-Key automatically

You can achieve this by using a panel hook in site/config/config.php

kirby()->hook('panel.page.update', function($page) {
  try {
    $page->update(array(
      'URL-Key' => str::slug($page->title()),
    ));
  } catch(Exception $e) {
    echo $e->getMessage();
  }
  return;
});

There are some Problems

  • It only works with languages which are not the default languages (as discussed here).
  • The URL-Key gets updated each time the title is changed. Why? You can’t use panel.page.create because this hook is fired when you change the language in the panel to create the page in the new language and at this moment the fields are prepopulated with the values from the default language. And this leads to an url key in the wrong (default) language …

But it was fun to check this out :smile:

2 Likes