How to exclude sub fields from rendering in a form

Created by Phil Kurth, Modified on Mon, 16 Jan, 2023 at 1:29 PM by Phil Kurth

Whilst Advanced Forms does have built-in support for excluding fields when rendering a form, this is currently limited to top level fields – the exclude_fields form argument is not evaluated against subfields such as those within groups, repeaters, or flexible content fields.


If you need to exclude subfields, you may do so by implementing the following plugin. If you would rather use this code directly, this code can be copied and pasted into a functions.php file, a custom plugin, or even a snippet manager — just be sure to remove the plugin headers (comments). 

The plugin hooks into the Advanced Forms' rendering process to intercept and modify the ACF field array on the fly. It behaves recursively and will loop through and sub_fields arrays it finds. If a field's (or sub-field's) name or key is in the form's exclude_fields argument, it will be excluded from form rendering. 


<?php
/**
 * Plugin Name: Advanced Forms Exclude Sub Fields Mod
 * Plugin URI:  https://hookturn.io
 * Description: Modifies Advanced Forms Pro to support sub field exclusion via form args.
 * Version:     0.1
 * Author:      Phil Kurth
 * Author URI:  http://philkurth.com.au
 */

// If this file is called directly, abort.
defined( 'WPINC' ) or die();

// Hook the custom modifier before rendering any fields on the form.
add_action( 'af/form/before_fields', function ( $form, $args ) {
	global $afesfm;
	// Set form render args to the global variable so we can access them in the my_custom_field_excluder() function.
	$afesfm['args'] = $args;

	add_filter( 'acf/load_fields', 'afesfm_modify_fields_array' );
}, 10, 2 );

// Remove the filter after the fields have been rendered.
add_action( 'af/form/after_fields', function ( $form, $args ) {
	remove_filter( 'acf/load_fields', 'afesfm_modify_fields_array' );
}, 10, 2 );

// Hooked function that modifies the ACF fields array and removes any fields (including sub fields) that are in the
// form's `exclude_fields` arg.
function afesfm_modify_fields_array( $fields ) {
	return array_filter( $fields, function ( $field ) {
		global $afesfm;

		if ( empty( $afesfm['args']['exclude_fields'] ) ) {
			return true;
		} else {
			$excludes = $afesfm['args']['exclude_fields'];
		}

		// If the field is marked for exclusion, remove it.
		if ( in_array( $field['key'], $excludes ) or in_array( $field['name'], $excludes ) ) {
			return false;
		}

		// Add support for any field type with sub fields.
		if ( isset( $field['sub_fields'] ) and is_array( $field['sub_fields'] ) ) {
			$field['sub_fields'] = afesfm_modify_fields_array( $field['sub_fields'] );
		}

		return true;
	} );
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article