acf_field_select::append_user_choices_to_field
Appends submitter-contributed values to a persisted field's choices array, subject to the acf/fields/max_appended_choices cap. Used by checkbox save_custom and select save_options. Callers must have already verified that the setting is enabled and that the field has an ID (i.e. it's not local/JSON-only).
Метод класса: acf_field_select{}
Хуки из метода
Возвращает
void. Ничего (null).
Использование
$acf_field_select = new acf_field_select(); $acf_field_select->append_user_choices_to_field( $value, $post_id, $field );
- $value(массив) (обязательный)
- Submitted values.
- $post_id(int|строка) (обязательный)
- The post_id of which the value will be saved.
- $field(массив) (обязательный)
- The persisted field array. Must have an ID.
Список изменений
| С версии 6.8.5 | Введена. |
Код acf_field_select::append_user_choices_to_field() acf field select::append user choices to field ACF 6.8.8
public function append_user_choices_to_field( $value, $post_id, $field ) {
/**
* Filters the maximum number of choices that may be stored on a field
* via the checkbox `save_custom`, radio `save_other_choice`, and select
* `save_options` settings. Once reached, additional submitter-contributed
* values are not appended to the field definition. The submitter's own
* field value still saves to their post.
*
* @since 6.8.5
*
* @param int $max Maximum number of choices. Default 1000.
* @param array $field The field array holding all the field options.
* @param integer|string $post_id The post_id of which the value will be saved.
*/
$max = (int) apply_filters( 'acf/fields/max_appended_choices', 1000, $field, $post_id );
$appended = false;
foreach ( $value as $v ) {
// Skip if the raw submitted value matches an existing key,
// preserving back-compat for fields whose choices contain
// un-normalized keys (e.g. developer-registered with HTML).
if ( isset( $field['choices'][ $v ] ) ) {
continue;
}
// Unslash (fixes serialize single quote issue) and sanitize.
$v = wp_unslash( $v );
$v = sanitize_text_field( $v );
// Skip if the normalized value is empty or already exists.
if ( $v === '' || isset( $field['choices'][ $v ] ) ) {
continue;
}
// Stop appending once the cap is reached.
if ( count( $field['choices'] ) >= $max ) {
break;
}
// Append to the field choices.
$field['choices'][ $v ] = $v;
$appended = true;
}
// Save only if we actually appended a new choice.
if ( $appended ) {
acf_update_field( $field );
}
}