How to add some text to page slug after saving it?


#1

Hello.

How can I add , at example, “2019-03-13-” to my page slug (would be useful for news or blog pages or something similar), after saving page, automatically? And get this value from my “date” field at example.

I think I need to find some hook to “save” event?

Thank you.


#2

You can set the num option in your blueprint. That way, whenever you publish a page, the date will be added automatically: https://getkirby.com/docs/reference/panel/blueprints/page#sorting

If you really want to publish a page as soon as it gets saved, you would have to use a hook, i.e. a page.update:after hook.


#3

Thank you, but num option wont’ change slug, it only add prefix to page folder. I need to change slug though…

Looks like I neet to make a hook…


#4

Yes, if you want the date in the slug, you have to use a hook. However, renaming a page via a hook will result in your current panel URL becoming invalid.


#5

So there is no “external” field to slug? Page slug in Kirby is it’s folder name only?


#6

Yes, the number (when publishing a page) doesn’t become part of the URL. What you could do, however, instead of adding the date to the slug, is use a route to catch a URL with the date in it and return the corresponding page.

To actually print that URL when linking to that page, you can change the URL method in a page model.


#7

Thank you :slight_smile:

Ok, as a compromise to my editors, I just set changeSlug option to true for this pages.


#8

Yes, if it is ok to do it manually. The hook option would be nicer but I’m currently not sure if and how to redirect to the new Panel URL from the hook.