# Update URL when page is create in Multi lang environnement

**URL:** <https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 11, 2020, 10:53am UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775 "2020-07-11T10:53:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 11, 2020, 10:53am UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/1 "2020-07-11T10:53:18Z")

</div>

Hi  
How to automatically update the URL (for example add the date of today of the timestamp) when a page is create ? I tried with a Hook in the config.php (panel.page.create) but it does not work. Maybe because I’m in a multi lang environnement (3 langagues).  
Does anyone can help us ? Thank you

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2020, 11:32am UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/2 "2020-07-11T11:32:15Z")

</div>

Could you post your code please?

---

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 11, 2020, 11:51am UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/3 "2020-07-11T11:51:45Z")

</div>

```
return [
'hooks' => [
    'page.create:before' => function ($page, $input) {
        $addToUrl = 'stringToAdd';
        $title = $page->title();
        $page->update(array(
            'URL-Key' => str::slug($title).$addToUrl,
        ));
        panel()->redirect($page, 'edit');
    }
]
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2020, 12:12pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/4 "2020-07-11T12:12:30Z")

</div>

You can’t update from a `before` hook, you have to use an `after` hook.

> [@lehorla13](#):
>
> `panel()->redirect($page, 'edit');`

That is not valid code. Where did you get that from?

---

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 11, 2020, 12:18pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/5 "2020-07-11T12:18:56Z")

</div>

Ok for the after Hook thank you.  
$page-\>update and ‘URL-Key’ is valid ?

No idea where panel()-\>redirect come from… Found it on the forum I think…  
How to finish a Hook like this one the best way ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2020, 12:33pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/6 "2020-07-11T12:33:48Z")

</div>

> [@lehorla13](#):
>
> How to finish a Hook like this one the best way ?

You don’t have to anything after the updating. Note that there is no such thing as a URL-Key for the default language.

There is an alternative to using a hook though: A page model that overrides the create method.

> [@lehorla13](#):
>
> No idea where panel()-\>redirect come from…

You probably mixed that up with old Kirby 2 code. Always doublecheck the thread categories and if old example code is still valid.

---

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 11, 2020, 12:48pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/7 "2020-07-11T12:48:17Z")

</div>

Ok good thank you  
So last question what is the parameter I have to update to change the URL ? If it’s not URL-key ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2020, 1:07pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/8 "2020-07-11T13:07:34Z")

</div>

The URL-Key is only useful for all non-default languages. Otherwise you would have to use the `changeSlug()` method. But note that if you change the slug in a hook, the user will not be redirected and get an error.

So changing the slug in a page model is the better option in this case.

```auto
class WhateverPage extends Page
{

    public static function create(array $props): Page
    {
        $props['slug'] = Str::slug('Hallo Welt');
        
        return parent::create($props);
    }
}

```

---

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 11, 2020, 10:56pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/9 "2020-07-11T22:56:38Z")

</div>

```
<?php
use Kirby\Cms\Page;
class EpisodePage extends Page
{
public function create(array $props): Page
{
    $props['slug'] = Str::slug('newUrl');

    return parent::create($props);
}
}

```

I tried like this but it looks like it does not affect the creation of the page (episode).

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 11, 2020, 11:30pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/10 "2020-07-11T23:30:03Z")

</div>

> [@lehorla13](#):
>
> `public function create`

It must be `public static function`

---

<div class="post-metadata">

**Author:** ![lehorla13](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lehorla13/32/12043_2.png) [@lehorla13](https://forum.getkirby.com/u/lehorla13)\
**Post date:** [July 17, 2020, 9:38pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/11 "2020-07-17T21:38:51Z")

</div>

Sorry but still not working with “static”

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 17, 2020, 10:51pm UTC](https://forum.getkirby.com/t/update-url-when-page-is-create-in-multi-lang-environnement/18775/12 "2020-07-17T22:51:50Z")

</div>

If you are on Kirby 3.4, you have to use the create method in the page model for tte page itself, not the parent. See also note here: [Hook page.create:before, how to edit user input?](https://forum.getkirby.com/t/hook-page-create-before-how-to-edit-user-input/12656/12)
