Writing values
3 min read
wpmcf_update_field()
wpmcf_update_field( string $group_key, string $field_key, mixed $value, mixed $object = null, array $args = [] ): boolStores a value the way the edit screen does: the field type validates and sanitizes it, the row in the group table is written, and page caches of the post are purged (wpmcf/cache/purge_post). For a post object it also assigns terms for Taxonomy fields set to "save terms".
Parameter | Description |
|---|---|
| The group and a top-level field that has a column. Layout fields (tab, accordion) have no value. Sub-fields are written through their container (pass the whole array). |
| The new value in the form |
| Same forms as for reading: Object references. The object must exist. |
|
|
Returns true on success, false when:
- the group, the field or the object does not exist, or the object reference is invalid;
- the field type refuses the value (for example a Password field longer than 128 bytes). A
_doing_it_wrong()notice with the reason is raised; - the write failed (the error is logged in WPM Fields › Settings › Health).
wpmcf_update_field( 'listing', 'price', 450000, $post_id );
wpmcf_update_field( 'listing', 'features', [ 'garden', 'garage' ], $post_id );
wpmcf_update_field( 'listing', 'details', [ 'city' => 'Rome' ], $post_id );
wpmcf_update_field( 'profile', 'bio', 'Hello', 'user_3' );
wpmcf_update_field( 'brand', 'color', '#ff6600', 'term_5' );
wpmcf_update_field( 'listing', 'price', null, $post_id ); // clearWhat "sanitized" means in practice
The type's own rules apply, exactly as on the edit screen. Values the type cannot accept are not reported as errors: they are dropped and the field is saved empty, and the function still returns true. Check the result by reading it back when it matters. Verified examples:
You write | Stored |
|---|---|
Text |
|
Number |
|
Checkbox |
|
Select value not in the choices ( |
|
URL | empty (the previous value is replaced by empty) |
WYSIWYG |
|
Password | nothing changes, returns |
See the Field types pages for each type's rules, and Password helpers and security for Password fields.
wpmcf_delete_field()
wpmcf_delete_field( string $group_key, string $field_key, mixed $object = null, array $args = [] ): boolClears the field's value (sets the column to NULL; the row and other fields stay). Returns true when the value was cleared or there was nothing to clear, false for an unknown group, field or an invalid object reference. Unlike wpmcf_update_field() it does not require the object to exist. wpmcf_delete_field( $g, $f, $id ) is the same as passing null to wpmcf_update_field().
Capabilities: there are none
add_action( 'wp_ajax_save_rating', function () {
check_ajax_referer( 'save_rating' );
$post_id = absint( $_POST['post_id'] ?? 0 );
if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_send_json_error( null, 403 );
}
$ok = wpmcf_update_field( 'review', 'rating', absint( $_POST['rating'] ?? 0 ), $post_id );
$ok ? wp_send_json_success() : wp_send_json_error();
} );If you want WordPress to do the permission check for you, use the REST API: it requires edit_post on the post.
Options pages
wpmcf_update_option_field( string $group_key, string $field_key, mixed $value ): bool
wpmcf_delete_option_field( string $group_key, string $field_key ): boolWrite to the shared options row of the group. wpmcf_update_option_field( 'g', 'f', $v ) equals wpmcf_update_field( 'g', 'f', $v, 'option' ). After a write, the action wpmcf/options_pages/values_saved fires once for every options page that shows the group, like a save on the page itself (it does not fire when no options page is registered for the group). No capability check.
wpmcf_update_option_field( 'site_settings', 'hours', 40 );Global Data
Global Data variables have their own writer, wpmcf_update_global(). See Global Data functions.
Multiple writes
Each call is one row upsert. To write several fields of the same object, call the function once per field; there is no transaction. Page caches are purged for each post write.
Limitations
- Only top-level fields with a column can be written. Writing
'details.city'is not supported; read the group, change the array and write the whole group. - The field type must be available on the site (for example a Pro type without an active license cannot be written).
- Writes trigger no
save_post. If other code must react, fire your own hook or usewpmcf/cache/purge_post. - A Password field is sealed according to its Store as setting, see Password.
Still stuck? Contact support