# Set Title & Slug to current time?

**URL:** <https://forum.getkirby.com/t/set-title-slug-to-current-time/31672>\
**Category:** Questions\
**Tags:** v4\
**Created:** [May 22, 2024, 2:46pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672 "2024-05-22T14:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevq](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kevq/32/13682_2.png) [@kevq](https://forum.getkirby.com/u/kevq)\
**Post date:** [May 22, 2024, 2:46pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672/1 "2024-05-22T14:46:20Z")

</div>

Hi folks,

I thought this would be a simple, but it appears not. What I would like to do is have the title and slug to be automatically generated when I create a “note” page within the panel.

In my ‘note’ blueprint I added the following:

```auto
create:
  title: <?php echo date('Ymd-Hi'); ?>
  slug: <?php echo date('Ymd-Hi'); ?>

```

But it seems that kirby won’t parse the php. So I tried doing it with a hook too, so I change the “create” options to something simple, like “note”:

```auto
create:
  title: note
  slug: note

```

Then have a hook to change the title/slug after the page is created:

```auto
'hooks' => [
        'page.create:after' => function ($newPage) {
            // Check if the created page is of the template 'note'
            if ($newPage->intendedTemplate() === 'note') {
                // Get the current date/time in the desired format
                $dateTime = date('Ymd-Hi');
                
                // Update the title and slug of the page to match the current date/time
                $newPage->update([
                    'title' => $dateTime,
                    'slug' => $dateTime
                ]);
            }
        }
    ]

```

But this doesn’t work either. The title/slug just remain as “note”. Clearly I’m being an idiot, but I can’t work this seemingly simple thing out.

Thanks!

---

<div class="post-metadata">

**Author:** ![kevq](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kevq/32/13682_2.png) [@kevq](https://forum.getkirby.com/u/kevq)\
**Post date:** [May 22, 2024, 2:50pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672/2 "2024-05-22T14:50:43Z")

</div>

I also tried using this:

```auto
create:
  title: '{{ page.published.toDate("Ymd-Hi") }}' 
  slug: '{{ page.published.toDate("Ymd-Hi") }}'

```

But it didn’t work either. I assume that’s because the page was still being created, so the `published` field wasn’t yet populated. So I tried this:

```auto
create:
  title: '{{ now.toDate("Ymd-Hi") }}'
  slug: '{{ now.toDate("Ymd-Hi") }}'

```

That was another fail, lol.

---

<div class="post-metadata">

**Author:** ![lkhrs](https://avatars.discourse-cdn.com/v4/letter/l/82dd89/32.png) [@lkhrs](https://forum.getkirby.com/u/lkhrs)\
**Post date:** [May 22, 2024, 4:11pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672/3 "2024-05-22T16:11:12Z")

</div>

Hey Kev!

I followed [this section](https://getkirby.com/docs/reference/panel/blueprints/page#page-creation-dialog__automatic-title-and-slug) in the docs and got it working.

Create a new plugin:  
`site/plugins/date-to-slug/index.php`

Add this code to `index.php`:

```php
<?php

Kirby::plugin('getkirby/date-to-slug', [
  'siteMethods' => [
    'time' => function() {
      return date('Ymd-Hi');
    }
  ]
]);

```

In your note blueprint, update the fields:

```yaml
create:
  title: "{{ site.time }}"
  slug: "{{ site.time }}"

```

This is the result after creating a new note page:

 ![Screenshot 2024-05-22 at 11.08.35](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/e/eafee10b56d20f3bc04ea9f1678eb1c0129c000b.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:** [May 22, 2024, 5:39pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672/4 "2024-05-22T17:39:52Z")

</div>

The other option would be to include the date field (`published`) in the create dialog.

---

<div class="post-metadata">

**Author:** ![kevq](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/kevq/32/13682_2.png) [@kevq](https://forum.getkirby.com/u/kevq)\
**Post date:** [May 22, 2024, 8:13pm UTC](https://forum.getkirby.com/t/set-title-slug-to-current-time/31672/5 "2024-05-22T20:13:48Z")

</div>

Huzzah! That worked, thanks Luke.

I changed the plugin slightly so it’s this:

```auto
<?php

Kirby::plugin('getkirby/date-to-slug', [
  'siteMethods' => [
    'time' => function() {
      return date('Ymd-Hi');
    },
    'pretty-time' => function() {
        return date('j F Y \a\t H:i');
      }
  ]
]);

```

Then added the following to my blueprint:

```auto
create:
  title: "{{ site.pretty-time }}"
  slug: "{{ site.time }}"

```

This way the slug is something like `20240522-2108` but the title is `22 May 2024 at 21:08`.

Thanks again.
