Skip to content

Kirby 4.5.0

$page->update()

Updates the page data

$page->update(?array $input = null, ?string $languageCode = null, bool $validate = false): Kirby\Cms\Page

Parameters

Name Type Default
$input array|null null
$languageCode string|null null
$validate bool false

Return type

Kirby\Cms\Page

This method does not modify the existing $page object but returns a new object with the changes applied. Learn more →

Parent class

Kirby\Cms\Page

Example

Kirby's objects are immutable. That means, when you modify an object like $page, $file etc. using a method like update(), changeTitle() and so on, a new object is returned. Therefore, you have to store the returned object in a new variable to be able to further work with it.

try {

  $newPage = page('mypage')->update([
    'title'        => 'A new title',
    'text'         => 'Some text',
    'anotherfield' => 'Some more data'
  ]);

  echo 'The page has been updated:';

} catch(Exception $e) {

  echo $e->getMessage();

}

Updating a field by callback

$newPage = $page->update([
  'title' => function($title) {
    return Str::lower($title);
  }
]);

Multi-language site

Use the optional $lang parameter to specify the language you want to update.

try {

  $newPage = page('mypage')->update([
    'title'        => 'A new title',
    'text'         => 'Some text',
    'anotherfield' => 'Some more data'
  ], 'en');

  echo 'The page has been updated';

} catch(Exception $e) {

  echo $e->getMessage();

}