ACF Table Field Add-on

Output Table HTML

Methodes to Output Table HTML

the_table()

Just echos the table HTML.

the_table('acf_field_name');

get_table()

Returns the table HTML.

$table_html = get_table('acf_field_name');
echo $table_html;

get_field()

Use get_field() to get a table field data or by any other ACF way to retriev a field data. Provide the table field data to get_table() or the_table() to get or output the table HTML.

$table_data = get_field('acf_field_name');
echo get_table( $table_data );
the_table( $table_data );

Modifing the Table HTML by PHP Filters

Table HTML Elements Attributes

The following example adds the CSS class custom and an data attribute data-style to the table HTML element of the table field with the field name my_field_name.

add_filter( 'acf_tablefield/get_table/attr/table', function( $attributes, $param ) {

	/* Function Parameters:

		$attributes = array(
			'class' => (array),
			'style' => (array),
		);

		$param = array(
			'post_id' => (integer),
			'field' => (array), // ACF fields data
			'selector' => (string), // Alias for the ACF field name
			'section_key' => (string), // 'thead', 'tbody', 'tfoot', empty on table and caption filter
			'item_data' => (array),
			'table_data' => (array),
		);
	*/

	if ( 'my_field_name' !== $param['selector'] ) {

		return $attributes;
	}

	$attributes['class'][] = 'custom';
	$attributes['data-style'] = 'dark';

	return $attributes;
}, 10, 2 );

With the following attribute filter variants with the same filter parameters from the example above, you could selectiv extend and add attributes for all HTML elements inside a table.

add_filter( 'acf_tablefield/get_table/attr/caption', …
add_filter( 'acf_tablefield/get_table/attr/section', …
add_filter( 'acf_tablefield/get_table/attr/row', …
add_filter( 'acf_tablefield/get_table/attr/cell', …