I am working on an events section on a site, and am wondering how to handle the slugs for the pages. For instance, there may be several called “Yoga Workshop”, but after the first with the slug yoga-workshop is created, adding another will trigger the “A page with the URL appendix “yoga-workshop” already exists” warning.
What is the most editor-friendly way of addressing this? I guess it must come up with news sections as well with titles like “Weekly Update” etc. Ideally I don’t want the editor to have to worry about this
I would think about a new strategy. Use more meaningful titles that are also optimized for SEO. The title “Yoga workshop” doesn’t say so much about the event. Add more information to the title:
That is certainly an option, and I appreciate the SEO benefits, but I think given the potential lifetime of the site that the potential for identical event titles is high. I want to remove the issue of an editor having to try various options to find a slug that hasn’t been used previously.
I think something like using the event start date as part of the slug would be a good solution, but I don’t think that is possible even with the new page creation dialogue options.
Here is a similar post, but not with a code example, just a suggested solution:
One idea is to automatically change the slug as soon as the event has ended and the page has a different status. Adding a timestamp would be one way to avoid duplicate titles in the future.
What you could do is create a page model that automatically adds a date or unix timestamp after the title (overwriting the $page::create() method.
I wouldn’t use a hook for that, because when you change the slug after page creation (which happens in a hook), then you will not be automatically redirected to the newly created page, but editors will be shown an error that the page doesn’t exist.
<?php
// Learned this from https://forum.getkirby.com/t/auto-append-to-url-appendix/19722
// on August 28, 2023.
// Found it googling 'kirby cms append number to slug'.
// Yay! Page models FTW!!!!!
// For a content file called 'event.txt'
// In general the class name is {{ShowFileName}}Page
use Kirby\Cms\Page;
use Kirby\Toolkit\Str;
class EventPage extends Page
{
// all methods of the Page class are inherited and can be overridden here now.
public static function create(array $props): Page
{
// $props['slug'] = Str::slug('Hello World'); ///// zu Testzwecken :)
$page = new Page($props);
if ( kirby()->page( $page->id() ) ) {
$props['slug'] = $props['slug'] . '-' . Str::random(16);
}
return parent::create($props);
}
}
I left my comments in there to remember like in a year or so … if I ever have to revise this … what I have done.
Thanks everyone this is really useful. I am going to set up a page model to handle this, and add a unix timestamp to ensure individuality.
If I were to add the event start date field to the Page Creation dialog, would it be possible to access this as part of the page model? As that would be ideal to add to the slug, rather than a random number or a timestamp
Then the user would still get an error message and could correct it. So dealing with this depends really on the probability that this might happen, i.e. two yoga workshops with the same title on the same startdate, rather unlikely and maybe even an error that should be surfaced?