ACF filters

<?php
/**
 * Populate ACF Select Field with WooCommerce Attributes.
 */
add_filter(
	'acf/load_field/name=featured_attributes',
	function ($field) {
		$taxonomies = get_taxonomies(
			[
			'_builtin' => false,
			],
			'objects'
		);

		$field['choices'] = [];

		/**
     * @var \WP_Taxonomy $taxonomy
     */
		foreach ($taxonomies as $taxonomy) {
			// Exclude non-woocommerce choices:
			if (strncmp('pa_', $taxonomy->name, 3) !== 0) {
				continue;
			}

			$field['choices'][$taxonomy->name] = str_replace("Product ", "", $taxonomy->label);
		}

		return $field;
	}
);


/**
 * Populate ACF Select Field with User roles
 */
add_filter(
	'acf/load_field/name=wc_edit_roles',
	function ($field) {
		$field['choices'] = array();

		global $wp_roles;

		$all_roles = $wp_roles->roles;
		unset($all_roles['administrator']);

		if (is_array($all_roles)) {
			foreach ($all_roles as $name => $values) {
				$field['choices'][$name] = $values['name'];
			}
		}

		return $field;
	}
);


// remove the html filter on all ACF fields
add_filter('acf/allow_unfiltered_html', '__return_true');
// re-apply filter to non-code fields
add_filter(
	'acf/update_value',
	function ($value, $post_id, $field) {
		if (substr($field['name'], -7) !== 'scripts') { // only apply wp_kses_post to fields (name) not ending with 'scripts'
			if ($field['type'] !== 'tab'
				&& $field['type'] !== 'group'
				&& $field['type'] !== 'repeater'
				&& $field['type'] !== 'flexible_content'
				&& $field['type'] !== 'clone'
				&& !is_array($value) && !is_object($value)
			) {
				$value = wp_kses_post($value);
			}
		}
		return $value;
	},
	1,
	3
);
Last Updated:
Contributors: Niek Vlam, Suite Seven