mjut Posted yesterday at 12:51 PM Share Posted yesterday at 12:51 PM Hello! I am trying to get a self generated name for new pages containing the creation-date and the title. At the moment, I am having a template set up with a name format for children like so: date:Y-m-d It works fine, the page name is generated and shows the URL like this domain.com/2024-11-25/ Is it possible to write the title name to the page name too? Like so: domain.com/2024-11-25-hello-world/ I cannot find a simple solution to it in the forums... I might just be looking at the wrong places? It seems so simple, but I just cannot get it working.... Thanks for some input/thoughts! Link to comment Share on other sites More sharing options...
Nicolas Posted 4 hours ago Share Posted 4 hours ago You could use the Pages::saved hook to format a page's name. You can find a example of code here : Link to comment Share on other sites More sharing options...
mjut Posted 2 hours ago Author Share Posted 2 hours ago Cool! Thank you @Nicolas ! I did modify that code slightly. I put it into ready.php: wire()->addHookBefore("Pages::published(template=article)", function($hook) { $page = $hook->arguments(0); if($page->skipMyHook) { // if the page already has our temporary property, we skip further processing return; } $newUrl = date("Y-m-d-") . wire()->sanitizer->pageName($page->title) ; // give it a name used in the url for the page wire()->log->message($page->title); $page->skipMyHook = true; // add a temporary property to the page (it is only in memory) $page->setAndSave('name', $newUrl); }); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now