# Problem with ChangeSlug() in hook

**URL:** <https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 9, 2021, 6:50pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534 "2021-03-09T18:50:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 6:50pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/1 "2021-03-09T18:50:47Z")

</div>

I just have the strange problem that the slug cannot be changed with the page hook. Is there something wrong with this code? The strange thing is that everything is created correctly in the file system, but the pages always have a "Slug: " in the text file. This means that I can never call the pages. Even if I click on Preview in the panel, I can’t view the page. It is in a multilingual environment.

```
'page.create:after' => function ($page) {
  if ( $page->intendedTemplate() == 'essigfuehrung' ) {
    $title = $page->fuehrung_datum()->toDate('d.m.Y - H:i') . ' Uhr';
    $slug = $page->fuehrung_datum()->toDate('YmdHi');
    $page->changeTitle($title);
    $page->changeSlug($slug);
  }
}
```

---

<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:** [March 9, 2021, 6:56pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/2 "2021-03-09T18:56:28Z")

</div>

The slug is changed in a non-default language in the content file, shouldn’t happen for the default language though.

But changing the slug in a hook is not a good idea, anyway.

Please stick with English in this forum, I corrected your topic title.

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 7:00pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/3 "2021-03-09T19:00:53Z")

</div>

Sorry for the title. But what’s a better way to change the slug instead of a Hook? I use the AddFields Plugin that the User simply pick a date and the slug must be generated through that field.

 ![Bildschirmfoto 2021-03-09 um 20.00.08](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/7/70844b4fe419427b5d0f70222f223a893632748e.png)

---

<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:** [March 9, 2021, 7:04pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/4 "2021-03-09T19:04:47Z")

</div>

> [@Oli1](#):
>
> But what’s a better way to change the slug instead of a Hook? I

That’s better off in a page model. Overwrite the create method.

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 7:07pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/5 "2021-03-09T19:07:31Z")

</div>

Is the page model called when the page is created? To be honest, I didn’t really understand about the page model. I create a page model with the same name as the template and then how is it called?

I have tried this but not sure how that works.

> **[steirico/kirby-plugin-custom-add-fields - Using custom Add Fields in Hook Scripts](https://github.com/steirico/kirby-plugin-custom-add-fields#using-custom-add-fields-in-hook-scripts)**
>
> Custom fields for Kirby's add dialog. Contribute to steirico/kirby-plugin-custom-add-fields development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 7:14pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/6 "2021-03-09T19:14:47Z")

</div>

I start to think that this is a bug of the Plugin. Even when I do not a hook, the plugin creates the hook with a timestamp. But same problem here …

---

<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:** [March 9, 2021, 7:17pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/7 "2021-03-09T19:17:30Z")

</div>

> [@Oli1](#):
>
> Is the page model called when the page is created?

yes

Example: [Auto-append to URL-Appendix - #3 by demlak](https://forum.getkirby.com/t/auto-append-to-url-appendix/19722/3)

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 7:18pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/8 "2021-03-09T19:18:56Z")

</div>

Only in this plugin or general? I thought the Page Model is for frontend?

---

<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:** [March 9, 2021, 7:21pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/9 "2021-03-09T19:21:35Z")

</div>

In a page model you extend the Page class The model inherits all public and protected methods from this class (class inheritance) and you can add new methods or overwrite existing ones.

If you are not familiar with OOP, you can read more about it here:

> **[A brief intro to object oriented programming in PHP](https://getkirby.com/docs/cookbook/templating/understanding-oop)**
>
> Understanding classes, objects, member functions and common pitfalls

---

<div class="post-metadata">

**Author:** ![Oli1](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/oli1/32/14419_2.png) [@Oli1](https://forum.getkirby.com/u/Oli1)\
**Post date:** [March 9, 2021, 7:25pm UTC](https://forum.getkirby.com/t/problem-with-changeslug-in-hook/21534/10 "2021-03-09T19:25:07Z")

</div>

Ahhh ok! Yes I have read that Cookbook, it’s really amazing!!! Ok I will give a try to it … not sure if my skills are enough for this.
