acf_inline_toolbar_editing_attrs()ACF 1.0

Helper function that returns the HTML attributes required for toolbar inline editing as a string or array.

Returns

String|Array. When $args['return_array'] is false (default): Returns a string of escaped HTML attributes ready for output. When $args['return_array'] is true: Returns an associative array of attribute names and escaped values. When using the array return value with wp_get_attachment_image(), no element will be rendered if the image field is empty. If users need an inline editing target for selecting an image, render a fallback element with the attributes returned by this function.

Usage

acf_inline_toolbar_editing_attrs( $fields, $args );
$fields(array) (required)
Array { Required. A list of the fields, each of which which will be displayed in the popup toolbar.

Each field can be passed as:

  • A string (e.g. 'my_field_name')
  • An associative array with specific keys:
@type string  $field_name  The name of the field to display in the toolbar.
@type string  $field_icon  An html tag, can be an svg, to be used as the toolbar icon. If not passed, the icon of the first field will be used.
@type string  $field_label A string to use as the label for the button in the toolbar.
@type boolean $use_expanded_editor Default is false, which opens the field in the popover. Set to true to open in the expanded editor.
@type string  $popover_min_width Enter the CSS width value to use for the popover.

}.
Default: "300px"

$args(array)
Array { Optional. An array of additional args which can control how the toolbar is displayed and used.
@type string  $toolbar_icon  Optional. An html tag, can be an svg, to be used as the toolbar icon. If not passed, the icon of the first field will be used.
@type string  $toolbar_title Optional. A string to be used as the toolbar title. If not passed, the name of the first field will be used.
@type string  $uid           Optional. A unique identifier that isn't used by any other inline fields in this block. Pass if you have 2 elements that conflict.
@type boolean $return_array  Optional. If true, returns an array of attributes suitable for wp_get_attachment_image().

}.
Default: false

acf_inline_toolbar_editing_attrs() code ACF 6.8.8

function acf_inline_toolbar_editing_attrs( $fields, $args = array() ) {

	$default_args = array(
		'toolbar_icon'  => null,
		'toolbar_title' => null,
		'uid'           => null,
		'return_array'  => false,
	);

	$args = wp_parse_args( $args, $default_args );

	// Helper for consistent early returns based on return format
	$empty_return = $args['return_array'] ? array() : '';

	if ( empty( $fields ) ) {
		return $empty_return;
	}

	$acf_block_version = acf_get_data( 'acf_current_block_version' );

	if ( ! $acf_block_version || $acf_block_version <= 2 ) {
		return $empty_return;
	}

	$render = acf_get_data( 'acf_doing_block_preview' );

	if ( ! $render ) {
		return $empty_return;
	}

	// Get the block id.
	$meta_instance = acf_get_instance( 'ACF_Local_Meta' );
	$block_id      = $meta_instance->post_id;

	if ( empty( $block_id ) || strpos( $block_id, 'block_' ) !== 0 ) {
		return $empty_return;
	}

	// Prefix the generated uid with the blockid.
	$generated_uid = $block_id;

	$fields_processed = array();

	/**
	 * Filters the field types that should open in the expanded editor by default.
	 *
	 * @since 6.7.0
	 *
	 * @param array $field_types An array of field type names.
	 * @return array
	 */
	$fields_to_open_in_expanded_editor = apply_filters(
		'acf/blocks/fields_to_open_in_expanded_editor',
		array(
			'repeater',
			'flexible_content',
		)
	);

	/**
	 * Filters the field types that require a wider popover for inline editing.
	 *
	 * @since 6.7.0
	 *
	 * @param array $field_types An array of field type names.
	 * @return array
	 */
	$fields_needing_wide_popover = apply_filters(
		'acf/blocks/fields_needing_wide_popover',
		array(
			'gallery',
			'relationship',
			'wysiwyg',
			'google_map',
		)
	);

	$popover_min_width_normal = '300px';
	$popover_min_width_wide   = '600px';

	foreach ( $fields as $field ) {
		if ( is_array( $field ) ) {
			$full_field_data = acf_get_field( $field['field_name'] );
			if ( $full_field_data ) {
				$use_expanded_editor_default = in_array( $full_field_data['type'], $fields_to_open_in_expanded_editor, true ) ? true : false;
				$popover_min_width_default   = in_array( $full_field_data['type'], $fields_needing_wide_popover, true ) ? $popover_min_width_wide : $popover_min_width_normal;

				$generated_uid     .= $field['field_name'];
				$fields_processed[] = array(
					'fieldName'         => $field['field_name'],
					// Base64 allows us to embed html in an attribute without causing issues with quotes, etc.
					'fieldIcon'         => ! empty( $field['field_icon'] ) ? base64_encode( $field['field_icon'] ) : null, // phpcs:ignore
					'fieldLabel'        => ! empty( $field['field_label'] ) ? $field['field_label'] : $full_field_data['label'],
					'useExpandedEditor' => ! empty( $field['use_expanded_editor'] ) ? $field['use_expanded_editor'] : $use_expanded_editor_default,
					'popoverMinWidth'   => ! empty( $field['popover_min_width'] ) ? $field['popover_min_width'] : $popover_min_width_default,
				);
			}
		} else {
			$full_field_data = acf_get_field( $field );

			if ( $full_field_data ) {
				$fields_processed[] = array(
					'fieldName'         => $field,
					'fieldIcon'         => null,
					'fieldLabel'        => $full_field_data['label'],
					'useExpandedEditor' => in_array( $full_field_data['type'], $fields_to_open_in_expanded_editor, true ) ? true : false,
					'popoverMinWidth'   => in_array( $full_field_data['type'], $fields_needing_wide_popover, true ) ? $popover_min_width_wide : $popover_min_width_normal,
				);
			}

			$generated_uid .= $field;
		}
	}

	if ( empty( $args['uid'] ) ) {
		$args['uid'] = $generated_uid;
	}

	// Build the attributes array.
	$attributes = array(
		'data-acf-inline-fields-uid' => $args['uid'],
		'data-acf-inline-fields'     => wp_json_encode( $fields_processed ),
		'role'                       => 'button',
		'tabindex'                   => '0',
	);

	if ( ! empty( $args['toolbar_icon'] ) ) {
		$attributes['data-acf-toolbar-icon'] = $args['toolbar_icon'];
	}

	if ( ! empty( $args['toolbar_title'] ) ) {
		$attributes['data-acf-toolbar-title'] = $args['toolbar_title'];
	}

	// Return as array if requested.
	if ( $args['return_array'] ) {
		return array_map( 'esc_attr', $attributes );
	}

	// Build and return as string (default behavior).
	$output = '';
	foreach ( $attributes as $key => $value ) {
		$output .= $key . '="' . esc_attr( $value ) . '" ';
	}
	return rtrim( $output );
}