# Page.create:after hook doesn't work as expected

**URL:** <https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948>\
**Category:** Questions\
**Created:** [July 7, 2022, 10:18pm UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948 "2022-07-07T22:18:54Z")\
**Posts on this page:** 5\
**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:** [July 7, 2022, 10:18pm UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948/1 "2022-07-07T22:18:54Z")

</div>

I’ve been trying to get the hook to work for ages but I keep getting error messages. I know that Kirby objects are immutable but I can’t find a solution that works. Does anyone know what I’m doing wrong and how I should deal with the immutable objects? I have already cached them in variables but somehow something is missing … (Plugin for AddFields is active)

```php
'page.create:after' => function ($page, $input) {
  if ( $page->intendedTemplate() == 'patient' ) {
    $kirby = kirby();
    try {
      $page->createChild([
        'slug' => 'nachrichten',
        'template' => 'nachrichten',
        'draft' => false,
        'content' => [
          'title' => 'Nachrichten'
        ]
      ]);
    } catch (Exception $e) {
      echo $e->getMessage();
    }
    try {
      $page->update([
        'vorname' => $page->vorname(),
        'nachname' => $page->nachname()
      ]);
    } catch(Exception $e) {
      echo $e->getMessage();
    }
    $title = $page->nachname() . ' ' . $page->vorname();
    $slug = bin2hex(random_bytes(25));
    $new = $page->changeTitle($title);
    $new->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:** [July 8, 2022, 3:25am UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948/2 "2022-07-08T03:25:37Z")

</div>

1. The hook doesn’t have a second parameter $input, only $page

> [@Oli1](#):
>
> ```auto
> $page->update([
> 'vorname' => $page->vorname(),
> 'nachname' => $page->nachname()
> ]);
> 
> ```

What the purpose of updating the page with data that already exists.

And you would have to store the result of this action in a new variable.

> [@Oli1](#):
>
> ` $slug = bin2hex(random_bytes(25));`

If you change the slug in a hook, you will not be redirected to the new location and get an error message in the Panel. Or do you get a different error message than “Page cannot be found”?

If you change the slug and title (or any other fields) in a page model (overwrite `create()` method) instead, you won’t run into this issue.

---

<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:** [July 8, 2022, 9:06am UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948/3 "2022-07-08T09:06:50Z")

</div>

Hi Sonja,

Thank you very much. Yes, updating the page with the same data makes no sense, it was more of an experiment. But even without that I get the message that I am not authorised to change the page, see screenshot. Or do I have to work with Impersonate here? That I’m not redirected is ok, I know that cause of the AddFields Plugin.

Do you have an example for the page model?

 ![Bildschirmfoto 2022-07-08 um 11.03.15](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/9/91df674da5e55329893058ef5431c136260b0f47.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:** [July 8, 2022, 9:25am UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948/4 "2022-07-08T09:25:31Z")

</div>

You have to check your blueprint permissions

---

<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:** [July 8, 2022, 9:51am UTC](https://forum.getkirby.com/t/page-create-after-hook-doesnt-work-as-expected/25948/5 "2022-07-08T09:51:50Z")

</div>

🤯 I was not aware that the Blueprint has an effect on the hooks. Learned something again, thank you
