Overview
Allow the [acf] shortcode and ACF block bindings to output full User field data.
Description
By default, the [acf] shortcode and ACF block bindings output only user IDs for User fields, so personal data such as names and email addresses isn’t shown to visitors. This applies whatever the field’s return format.
Returning true skips this. Only values that can be shown as text are output, so a single User field set to return User Array outputs its data as a comma-separated list. User Object fields, and multiple users returned as arrays, output nothing.
Parameters
apply_filters( 'acf/bindings/allow_user_data', $allow, $field, $post_id, $context );
$allow
(bool) Whether to output full User field data. Default: false
$field
(array) The field array containing all settings.
$post_id
(int|string) The ACF post ID the value is loaded from.
$context
(string) Where the value is output. Either 'shortcode' or 'bindings'.
Example
Show a user’s display name instead (recommended)
Rather than using this filter, convert the user IDs into the text you want when they’re output. This only changes the shortcode and block bindings, so get_field() still returns the field’s normal value.
function my_user_ids_to_names( $value ) {
$names = array();
foreach ( explode( ',', (string) $value ) as $id ) {
$user = get_userdata( (int) $id );
if ( $user ) {
$names[] = $user->display_name;
}
}
return esc_html( implode( ', ', $names ) );
}
// The [acf] shortcode.
add_filter( 'do_shortcode_tag', function ( $output, $tag, $attr ) {
if ( 'acf' === $tag && is_array( $attr ) && 'post_contact' === ( $attr['field'] ?? '' ) ) {
return my_user_ids_to_names( $output );
}
return $output;
}, 10, 3 );
// ACF block bindings.
add_filter( 'acf/blocks/binding_value', function ( $value, $source_attrs ) {
if ( 'post_contact' === ( $source_attrs['key'] ?? '' ) ) {
return my_user_ids_to_names( $value );
}
return $value;
}, 10, 2 );
Restore full output for one field in the shortcode
add_filter( 'acf/bindings/allow_user_data', function ( $allow, $field, $post_id, $context ) {
if ( 'shortcode' === $context && 'post_contact' === $field['name'] ) {
return true;
}
return $allow;
}, 10, 4 );
Changelog
- Added in version 6.8.11
Notes
- Returning
trueshows the user’s personal data, including their email address, to every visitor who can see the output. Only do this if that’s what you intend. - This filter applies to both the shortcode and block bindings. Use
$contextto target one. - This filter only runs for User fields.
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
- Field Types: User
- Filters: acf/schema/format_value/pre/type={field_type}
- Filters: acf/format_value
- Filters: acf/fields/allow_append_choices
- Filters: acf/update_value