# Page.update:after hook is called when page is created (automatically populate createdOn/createdBy and lastUpdatedOn/lastUpdatedBy fields in a page)

**URL:** <https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 20, 2023, 3:15pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140 "2023-07-20T15:15:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_1](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Andreas\_1](https://forum.getkirby.com/u/Andreas_1)\
**Post date:** [July 20, 2023, 3:15pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/1 "2023-07-20T15:15:59Z")

</div>

Good afternoon,

so I want to automatically populate a createdOn/createdBy and a lastUpdatedOn/lastUpdatedBy fields in a page when the page is created or updated via the panel.

This works easily with the following hooks which I have put into my config.php (N.B. to everyone who tries the same as me beforehand, i.e. displaying these fields via blueprint in the panel - dont do it! It will mix up everything and it wont work or - more realistic - I made some error 🙂 ):

```auto
   'hooks' => [
        'page.create:after' => function (Kirby\Cms\Page $page) {
            $createdOn = Kirby\Toolkit\Date::now();
            $user = kirby()->user();
            $page->update([
                'createdOn' => $createdOn,
                'createdBy' => $user
              ]);

        },
        'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
            $updatedOn = Kirby\Toolkit\Date::now();
            $updatedBy = kirby()->user();
            $newPage->update([
                'lastUpdatedOn' => $updatedOn,
                'lastUpdatedBy' => $updatedBy
              ]);
        }
    ],

```

Now when I create a page via the Panel, the createdOn/createdBy fields are populated - wonderful! But also the lastUpdatedOn/lastUpdatedBy were, which should not be the case IHMO when creating a NEW page, right?

Am I doing something wrong or is this a bug?

Thanks  
Andreas

---

<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 20, 2023, 3:21pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/2 "2023-07-20T15:21:42Z")

</div>

> [@Andreas\_1](#):
>
> populate a createdOn/createdBy

For this, you don’t need a hook, but can set a default for these fields in your blueprint.

And the second hook is called because you update right after creation.

So not using the first hook and use default values will then also prevent your issue.

---

<div class="post-metadata">

**Author:** ![Andreas\_1](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Andreas\_1](https://forum.getkirby.com/u/Andreas_1)\
**Post date:** [July 20, 2023, 4:14pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/3 "2023-07-20T16:14:04Z")

</div>

Right thanks, but that means in the Panel (via blueprint) either I have to show the disabled createdOn/By fields or come up with a condition to never show them, right?

---

<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 20, 2023, 4:30pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/4 "2023-07-20T16:30:16Z")

</div>

If you don’t want to show the field value to anyone, use a `hidden` field type.

---

<div class="post-metadata">

**Author:** ![Andreas\_1](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Andreas\_1](https://forum.getkirby.com/u/Andreas_1)\
**Post date:** [July 20, 2023, 5:47pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/5 "2023-07-20T17:47:14Z")

</div>

Right but then I have to do some [customizing](https://forum.getkirby.com/t/hidden-field-date-default-value/26621), right? I try to avoid that. Actually, I think I will leave as is, I can live with it 🙂

---

<div class="post-metadata">

**Author:** ![trey](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/trey/32/11716_2.png) [@trey](https://forum.getkirby.com/u/trey)\
**Post date:** [December 14, 2023, 11:09pm UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/6 "2023-12-14T23:09:44Z")

</div>

This is exactly what I needed. Thanks, @Andreas_1!

---

<div class="post-metadata">

**Author:** ![BDavis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bdavis/32/14969_2.png) [@BDavis](https://forum.getkirby.com/u/BDavis)\
**Post date:** [June 14, 2024, 6:39am UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/7 "2024-06-14T06:39:23Z")

</div>

What is the default value for the blueprint, whereby you want to save the user?

I have the following field setup in my blueprint, but it does not fill the field with a value:

```auto
                        createdby:
                            type: users
                            label: Autor
                            multiple: false
                            default: "{{ kirby.user }}"
                            disabled: true

```

---

<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:** [June 14, 2024, 6:45am UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/8 "2024-06-14T06:45:44Z")

</div>

To store the current user and if you are on Kirby 4 (not 3 as the category of this thread), then `default: true` would be the correct syntax.

---

<div class="post-metadata">

**Author:** ![BDavis](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bdavis/32/14969_2.png) [@BDavis](https://forum.getkirby.com/u/BDavis)\
**Post date:** [June 14, 2024, 7:08am UTC](https://forum.getkirby.com/t/page-update-after-hook-is-called-when-page-is-created-automatically-populate-createdon-createdby-and-lastupdatedon-lastupdatedby-fields-in-a-page/29140/9 "2024-06-14T07:08:47Z")

</div>

Thanks, that worked!
