Cybo Advanced Field EngineCyboweb Technologies · Documentation · v1.0.2

cafe_submit_dynamic_form()

Purpose

Validates and saves data through a dynamic form from PHP, with the same checks as a form submitted in the browser. Useful for imports, custom endpoints and tests.

Syntax

cafe_submit_dynamic_form( string|array $form, array $data ): Cybo\AdvancedFieldEngine\Forms\FormResult

Parameters

Parameter Type Default Description
$form string|array - A registered form ID, or form arguments (see cafe_dynamic_form()).
$data array - fields: values keyed by field name or key. post: WordPress post fields; title, content, excerpt, terms ([ taxonomy => [ term IDs ] ]).

Return value

An Cybo\AdvancedFieldEngine\Forms\FormResult:

Property Type Description
success bool Whether everything was saved.
object_id int The created or edited object (0 for options or when nothing was saved).
field_errors array Messages keyed by field key; post_title and post_terms_{taxonomy} for post fields.
form_errors string[] Messages about the whole form.
message string Success message.
redirect string Where the form goes after saving ("" = stay).
status int HTTP status used for REST answers (403 when not allowed, …).

to_array() returns the same data as the REST API.

Example

$result = cafe_submit_dynamic_form( 'new-event', [
	'post'   => [ 'title' => 'Summer fair', 'terms' => [ 'event_category' => [ 12 ] ] ],
	'fields' => [ 'venue' => 'Town hall', 'seats' => 120 ],
] );

if ( ! $result->success ) {
	foreach ( $result->field_errors as $key => $message ) {
		error_log( "$key: $message" );
	}
}

Notes

  • Runs as the current user: the same permission checks apply. There is no browser nonce, because the caller is PHP code.
  • Fields that are not part of the form are ignored.
  • The post and all values are saved in one database transaction; see Validation for what that covers.