cafe_update_field()
Purpose
Saves a field value. The value is validated and sanitized by the field type first.
Syntax
cafe_update_field( string $selector, mixed $value, mixed $object_id = null, ?string $object_type = null ): bool
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
$selector |
string | - | Field name or field key. |
$value |
mixed | - | New value. An empty value ('', null, []) removes the saved value. |
$object_id |
mixed | null |
Which object. null means the current object. |
$object_type |
string|null | null |
Object type, or null to infer it. |
Return value
true on success, false on failure (unknown field, unknown object, or invalid value).
Example
cafe_update_field(
'hero_title',
'Welcome',
$post_id
);
Need to know why a save failed? Use the service, which returns a WP_Error:
$result = cafe()->fields()->update( 'hero_title', $title, $post_id );
if ( is_wp_error( $result ) ) {
error_log( $result->get_error_message() );
}
Notes
- No permission check. Like
update_post_meta(), this is a developer function. Checkcurrent_user_can()yourself before saving user input. - The field's Required setting is not enforced here (so code can clear values).
Pass
trueas the fifth argument ofcafe()->fields()->update()to enforce it. - Fires
cafe_before_save_fieldandcafe_after_save_field. - The object must exist: saving for post
999999that does not exist fails.