Skip to content
Custom fields for WordPress that are quick to build and quick to load. Get Pro
Docs

The PHP API

Writing and deleting

1 minute to read Updated 1 week ago

PHP
ugly_update( 'price', 120 );                 // on the current post
ugly_update( 'price', 120, 42 );             // on post 42

ugly_update( 'team', array(                  // a whole repeater
	array( 'name' => 'Ana',  'role' => 'Chef' ),
	array( 'name' => 'Luis', 'role' => 'Baker' ),
), 42 );

ugly_delete( 'price', 42 );

What you write goes through the same cleaning as what the editor writes. A number out of range is cut. A badly typed color is dropped. A value that does not belong is not saved.

Both return true when something was written (or deleted) and false when it was not.

A common recipe: add one row to a repeater

Rows come out as objects, so convert each one to an array, add yours, and write the list back:

PHP
$rows = array_map(
	static fn ( $row ) => $row->toArray(),
	ugly_rows( 'team', 42 )
);

$rows[] = array( 'name' => 'Marta', 'role' => 'Waiter' );

ugly_update( 'team', $rows, 42 );

Did this article answer your question?