Auto Increment IDs in Kirby

Auto increment IDs in Kirby

I built an auto increment ID logic for kirby pages using hooks. The following code is self explaining:

// in the plueprint

// ...
// ... your bluprint rules
// ...

   label: Auto Increment ID
   type: number
   readonly: true
// site/config/config.php

// update the created page with the ID after creating the page
kirby()->hook('', function($page){
    // get the actual id from an txt file in the kirby root folder
    $aiid = intval(file_get_contents(kirby()->roots->index().'/aiid.txt'));

    // save the incremented id
    file_put_contents(kirby()->roots->index().'/aiid.txt', $aiid, LOCK_EX);

    // update the created kirby page
    $page->update(array('aiid' => $aiid));

Use Case Google News

To support the google news bot you can put an aiid in the url. I will use the ID for this case in a custom URL:

Extract from the technical guidelines of google news

Article URLs. To make sure we only crawl new articles, please make sure your URLs are unique with at least 3 digits, and are permanent.

Any ideas to improve/extend it? I am glad about your proposals.

One question: why do you fake the disabled state? readonly: true in your blueprint should work …

1 Like

Sorry, my fault. I didnΒ΄t know this option…