Overview
Allow extra field keys to be saved when ACF saves submitted field values.
Description
When ACF saves submitted field values, it only saves fields that were shown on the form for the content being saved, or whose field group’s location rules match that content. Any other submitted fields are not saved.
Use this filter to allow additional field keys, for example when your own JavaScript adds ACF inputs to a form, or when you render a field outside a field group whose location rules match the content.
The dynamic portion of the hook name, {context}, is the type of content being saved:
postusertaxonomycommentattachmentnav_menunav_menu_itemwidgetoptions_pagewoo_orderdatastoreblock_meta(ACF Blocks that save to post meta)
Parameters
apply_filters( "acf/form/{$context}/allowed_field_keys", $keys, $post_id );
$keys
(array) The top-level field keys allowed to save, such as field_abc123.
$post_id
(int|string|array) The object being saved. The user ID or comment ID for the user and comment contexts, the options page settings for the options_page context, and the ACF post ID for all other contexts. For the post and datastore contexts, this is the revision ID when saving a preview or autosave.
Example
Allow a field added to the post edit screen with JavaScript
add_filter( 'acf/form/post/allowed_field_keys', function ( $keys, $post_id ) {
// Previews and autosaves save to a revision, so check the post it belongs to.
$parent_id = wp_is_post_revision( $post_id );
if ( 'product' === get_post_type( $parent_id ? $parent_id : $post_id ) ) {
$keys[] = 'field_abc123';
}
return $keys;
}, 10, 2 );
Allow a field only for a specific post type
add_filter( 'acf/form/post/allowed_field_keys', function ( $keys, $post_id ) {
if ( 'product' === get_post_type( $post_id ) ) {
$keys[] = 'field_abc123';
}
return $keys;
}, 10, 2 );
Changelog
- Added in version 6.8.7 for the
userandcommentcontexts - Added in version 6.8.11 for all other contexts
Notes
- Keys are top-level field keys. Sub fields of a Repeater, Group, or Flexible Content field save with their parent field, so allow the parent’s key.
- To find out which fields aren’t being saved, enable
WP_DEBUGandWP_DEBUG_LOG, then save again. ACF logs a line starting withACF:for each save that drops fields, including the context and the field keys. - Before using this filter, check the field group’s location rules. Often the fix is to make them match where the field is used.
- Options pages also have the
acf/options_page/allowed_field_keysfilter, which passes the options page settings as its second argument. Front-end forms created withacf_form()have theacf/form/allowed_field_keysfilter, which passes the form settings.
Supercharge Your Website With Premium Features Using ACF PRO
Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.
Related
- Filters: acf/settings/enable_datastore
- Filters: acf/fields/allow_append_choices
- Features: Using the ACF Datastore
- Filters: acf/is_trusted_save_context
- Filters: acf/fields/relational/hide_unreadable_posts