# Create custom blocks programmatically

**URL:** <https://forum.getkirby.com/t/create-custom-blocks-programmatically/22137>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 1, 2021, 7:35pm UTC](https://forum.getkirby.com/t/create-custom-blocks-programmatically/22137 "2021-05-01T19:35:41Z")\
**Posts on this page:** 1\
**Showing post:** 12

<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 2, 2021, 10:26am UTC](https://forum.getkirby.com/t/create-custom-blocks-programmatically/22137/12 "2021-05-02T10:26:52Z")

</div>

Ok, I played around a bit more, and it seems to work when I convert the blocks to array when updating the page:

```auto
$newBlocks = new Kirby\Cms\Blocks();
$blocks = $page->text()->toBlocks();
foreach ($blocks as $block) :
  if ($block->type() !== 'image') {
    $newBlocks->add($block);
  } else {
    $url = $block->image()->toFile()->url();
    $images = $block->image()->toFiles()->pluck('filename');
    $newBlock = new \Kirby\Cms\Block(
      [
        "content" => [
          'location' => 'kirby',
          'image' => $images,
          'url' => $url,
          'alt' => $block->alt()->value(),
          'caption' => $block->caption()->value(),
          'link' => $block->link()->value(),
        ],
        'id' => $block->id(),
        'type' => $block->type()
      ],
    );
    $newBlocks->add($newBlock);
  }
endforeach;

$page->update(
  [
    'text' => $newBlocks->toArray(),
  ]
);

```

In the example above I added a new url field to the image block blueprint to store the url of the image.

---

_[View the full topic](https://forum.getkirby.com/t/create-custom-blocks-programmatically/22137)._
