Select plugin version:
ACFTablefield Class
This class allows reading, rendering and modifying a table as well as updating a table field.
Get ACFTablefield class instance
$tablefield = new ACFTablefield();
Get field
Loading a tablefields data into the class to work with.
Get field
$tablefield->get_field( $selector, [$post_id = false] );
Parameters
-
$selector
- Type: string
- Default:
- Description:
The field name or field key.
-
$post_id
- Optional: yes
- Type: mixed
- Default: false
- Description:
The post ID where the value is saved. Defaults to false, which will use the current post.
Return
Stores the field data in: ACFTablefield::$field_data
Get sub field
$tablefield->get_sub_field( $selector );
-
$selector
- Type: string
- Default:
- Description:
The field name or field key.
Return
Stores the field data in: ACFTablefield::$field_data
Cell content
Get cell content
$cell_content_string = $tablefield->get_cell_content( $column_index, $row_index, $table_section );
Cell walker
$tablefield->cell_walker( function( $cell_content, $data ) {
// $data['section']
// $data['row_index']
// $data['cell_index']
// $data['cell_options']
// $data['row_options']
// $data['column_options']
// $data['table_options']
return $cell_content;
});
The cell walker iterates over all cells in the table.
Update cell content
$tablefield->update_cell_content( $cell_content_string, $column_index, $row_index, $table_section );
Move
Move a column
$tablefield->move_column( $column_index, $target_index );
Move a row
$tablefield->move_row( $row_index, $target_index );
Add
Add a column
$tablefield->add_column( $column_index );
Add a row
$tablefield->add_row( $row_index );
Append
Append a column
$column_index = $tablefield->append_column();
Append a row
$row_index = $tablefield->append_row();
Remove
Remove a column
$tablefield->remove_column( $column_index );
Remove a row
$tablefield->remove_row( $row_index, $table_section );
Data
The data()
method works like an API. Data from different table sections can be read and changed.
data()
$param = array(
'action' => $action,
'name' => $name,
//'section' => $section, // dependently required
//'row' => $row_index, // dependently required
//'col' => $col_index, // dependently required
//'value' => $value, // dependently required
);
$result = $tablefield->data( $param );
Parameters
-
$action
- Type: string
- Options: get, update, add, remove
- Description:
Defines the action to perfom.
-
$name
- Optional: yes
- Type: string
- Default:
null
(returns all options) - Description:
Defines the data name. Some data need row and/or col parameter. If there is no name given, all data of the section are returned on a valid set of the other parameters.
-
$section
- Type: string
- Default:
null
- Options:
table
,columns
,head
,body
,foot
- Description:
Defines the data section to get the data from. This is dependently required.
-
$value
- Type: mixed
- Default:
null
- Description:
Defines a value to update, add or remove.
-
$row
- Optional: yes
- Type: integer
- Default:
null
- Description:
Defines the row of the table body section to get the data from. This is dependently required.
-
$col
- Optional: yes
- Type: integer
- Default:
null
- Description:
Defines the column to get the data from. This is optional required.
Return
On action 'get': returns the data value or an array of data name/value pairs. If no valid parameters were given or the data value not exists, null
is returned. On action 'update', 'add', 'remove': returns true
or false
depending on whether the data has been set.
Data actions
$action | Description | Data |
---|---|---|
get
|
Gets the data according to the other parameters. | all |
update
|
Updates the data by replacing it. If data not exists, it sets the data. | all |
add
|
Adds data to existing data. |
styles
|
remove
|
Removes data from existing data. |
styles
|
unset
|
Removes the data entry. | all |
Data sections
Section | Parameters | $name |
---|---|---|
Field parameter |
|
field_key field_name plugin_version
|
All data of the table |
|
|
A specific data of the table |
|
use_header use_footer use_stub_column caption cols rows content_type wp_editor_toolbar styles
|
All data of a table column |
|
|
A specific data of a table column |
|
content_type wp_editor_toolbar styles col_width col_min_width
|
All data of the head row |
|
|
A specific data of the head row |
|
content_type wp_editor_toolbar styles
|
All data of a head cell |
|
|
A specific data of a head cell |
|
content_type wp_editor_toolbar styles colspan content
|
All data of the foot row |
|
|
A specific data of the foot row |
|
content_type wp_editor_toolbar styles
|
All data of a foot cell |
|
|
A specific data of a foot cell |
|
content_type wp_editor_toolbar styles colspan content
|
All data of a body row |
|
|
A specific data of the body row |
|
content_type wp_editor_toolbar styles
|
All data of a body cell |
|
|
A specific data of a body cell |
|
content_type wp_editor_toolbar styles colspan rowspan has_span content
|
Data names
Name | Section | Type | Values | Action | Description |
---|---|---|---|---|---|
content_type
|
table columns body head foot
|
string | null
|
none st ed
|
get, update |
Returns the content type. |
wp_editor_toolbar
|
table body columns head foot
|
string | null
|
basic full or custom toolbars
|
get, update |
Defines the editor toolbar. Requires |
styles
|
table body columns head foot
|
string | null
|
get, update, add, remove |
Wether the table stub column should be used. |
|
use_header
|
table
|
string |
y n
|
get, update |
Wether the table header should be used. |
use_footer
|
table
|
string |
y n
|
get, update |
Wether the table footer should be used. |
use_stub_column
|
table
|
string |
y n
|
get, update |
Wether the table stub column should be used. |
colspan
|
body head foot
|
integer | null
|
get, update |
Amount of columns to span. |
|
rowspan
|
body
|
integer | null
|
get, update |
Amount of rows to span. |
|
has_span
|
body head foot
|
array | null
|
Coordinates of the spanning cell and its colspan and rowspan values.
|
get, update |
If a cell is spanned, this returns an Array which contains the information about the column and row index of the originating cell and the span distances. |
caption
|
table
|
string | get, update |
The table caption content. |
|
col_width
|
body head foot
|
integer | null
|
get, update |
With of the column at the admin editing table. |
|
col_min_width
|
body head foot
|
integer | null
|
get, update |
Min with of the column at the admin editing table. |
|
content
|
body head foot
|
string | get, update |
The cell content. |
content_type
Get
$content_type = $tablefield->data( [
'action' => 'get',
'name' => 'content_type',
'section' => $section,
'row' => $row_index, // required for row data on section body
'col' => $col_index, // required for column data for section column and cell data for sections body, head, foot.
] );
Update
$tablefield->data( [
'action' => 'update',
'name' => 'content_type',
'section' => $section,
'value' => $value,
] );
Parameters
-
$section
- Options: table, columns, body, head, foot
Values
$value | Description |
---|---|
none | Table content not editable |
If no content type value is set, then the value is inherited from the parent setting. | |
st | Simple text editing |
ed | WordPress classic editor (customizable toolbar) |
wp_editor_toolbar
Get
$wp_editor_toolbar = $tablefield->data( [
'action' => 'get',
'name' => 'wp_editor_toolbar',
'section' => $section,
] );
Update
$tablefield->data( [
'action' => 'update',
'name' => 'wp_editor_toolbar',
'section' => $section,
'value' => $value,
] );
Parameters
-
$section
- Options: table, columns, body, head, foot
Values
$value | Description |
---|---|
If no WordPress editor toolbar value is set, then the value is inherited from the parent setting. A toolbar value is required for the table section. | |
basic | Basic toolbar |
full | Full WordPress editor toolbar |
Table
Get table option
$all_table_options = $tablefield->get( [ 'section' => 'table' ] );
$option_value = $tablefield->get( [ 'section' => 'table', 'name' => $data_name ] );
$content_type = $tablefield->get( [ 'section' => 'table', 'name' => 'content_type' ] ); // string
$wp_editor_toolbar = $tablefield->get( [ 'section' => 'table', 'name' => 'wp_editor_toolbar' ] ); // string
$use_header = $tablefield->get( [ 'section' => 'table', 'name' => 'use_header' ] ); // string
$use_footer = $tablefield->get( [ 'section' => 'table', 'name' => 'use_footer' ] ); // string
$use_stub_column = $tablefield->get( [ 'section' => 'table', 'name' => 'use_stub_column' ] ); // string
$styles = $tablefield->get( [ 'section' => 'table', 'name' => 'styles' ] ); // string
Set table option
$tablefield->set( [ 'section' => 'table', 'name' => $data_name, 'value' => $value ] );
$tablefield->set( $option_name, 'true' ); // set boolean type option true
$tablefield->set( $option_name, 'false' ); // set boolean type option false
$tablefield->set( $option_name, array( 'add', $option_value ) ); // adds value to array type option
$tablefield->set( $option_name, array( 'remove', $option_value ) ); // removes value from array type option
$tablefield->set( $option_name, array( 'update', $option_value_array ) ); // updates array type option
Column
Get column option
$all_column_options = $tablefield->get_column_option( '', $column_index );
$column_option = $tablefield->get_column_option( $option_name, $column_index );
Update column option
$tablefield->update_column_option( $option_name, $option_value, $column_index );
Body row
Get body row option
$all_body_row_options = $tablefield->get_body_row_option( '', $row_index );
$body_row_option = $tablefield->get_body_row_option( $option_name, $row_index );
Update body row option
$tablefield->update_body_row_option( $option_name, $option_value, $row_index );
Body cell
Get body cell option
$all_body_cell_options = $tablefield->get_body_cell_option( '', $row_index, $column_index );
$body_cell_option = $tablefield->get_body_cell_option( $option_name, $row_index, $column_index );
Update body cell option
$tablefield->update_body_cell_option( $option_name, $option_value, $row_index, $column_index );
Header row
Get header row option
$all_header_row_options = $tablefield->get_header_row_option();
$header_row_option = $tablefield->get_header_row_option( $option_name );
Update header row option
$tablefield->update_header_row_option( $option_name, $option_value );
Header cell
Get header cell option
$all_header_cell_options = $tablefield->get_header_cell_option( '', $cell_index );
$header_cell_option = $tablefield->get_header_cell_option( $option_name, $cell_index );
Update header cell option
$tablefield->update_header_cell_option( $option_name, $value, $cell_index );
Footer row
Footer cell
Field metas
Get field meta
$all_field_metas = $tablefield->get_field_meta();
$number_of_columns = $tablefield->get_field_meta( 'columns' );
$number_of_rows = $tablefield->get_field_meta( 'rows' );
$field_key = $tablefield->get_field_meta( 'field_key' );
$field_name = $tablefield->get_field_meta( 'field_name' );
$plugin_version = $tablefield->get_field_meta( 'plugin_version' );
Field settings
Get field setting
$all_field_seetings = $tablefield->get_field_setting();
// table
$default_table = $tablefield->get_field_setting( 'default_table' ); // returns raw field data
$min_columns = $tablefield->get_field_setting( 'min_columns' );
$max_columns = $tablefield->get_field_setting( 'max_columns' );
$min_rows = $tablefield->get_field_setting( 'min_rows' );
$max_rows = $tablefield->get_field_setting( 'max_rows' );
$move = $tablefield->get_field_setting( 'move' );
$move_columns = $tablefield->get_field_setting( 'move_columns' );
$move_rows = $tablefield->get_field_setting( 'move_rows' );
$body_content_type = $tablefield->get_field_setting( 'body_content_type' );
$body_content_type_editor = $tablefield->get_field_setting( 'body_content_type_editor' );
$stub_column = $tablefield->get_field_setting( 'stub_column' );
$table_header = $tablefield->get_field_setting( 'table_header' );
$table_footer = $tablefield->get_field_setting( 'table_footer' );
$table_styles = $tablefield->get_field_setting( 'table_styles' );
// columns
$column_size = $tablefield->get_field_setting( 'column_size' );
$column_content_type = $tablefield->get_field_setting( 'column_content_type' );
$column_content_type_editor = $tablefield->get_field_setting( 'column_content_type_editor' );
$column_styles = $tablefield->get_field_setting( 'column_styles' );
// body
$body_row_content_type = $tablefield->get_field_setting( 'body_row_content_type' );
$body_row_content_type_editor = $tablefield->get_field_setting( 'body_row_content_type_editor' );
$body_cell_span = $tablefield->get_field_setting( 'body_cell_span' );
$body_cell_span_column = $tablefield->get_field_setting( 'body_cell_span_column' );
$body_cell_span_row = $tablefield->get_field_setting( 'body_cell_span_row' );
$body_cell_content_type = $tablefield->get_field_setting( 'body_cell_content_type' );
$body_cell_content_type_editor = $tablefield->get_field_setting( 'body_cell_content_type_editor' );
$body_row_styles = $tablefield->get_field_setting( 'body_row_styles' );
$body_cell_styles = $tablefield->get_field_setting( 'body_cell_styles' );
// header
$header_row_content_type = $tablefield->get_field_setting( 'header_row_content_type' );
$header_row_content_type_editor = $tablefield->get_field_setting( 'header_row_content_type_editor' );
$header_cell_span = $tablefield->get_field_setting( 'header_cell_span' );
$header_cell_span_column = $tablefield->get_field_setting( 'header_cell_span_column' );
$header_cell_content_type = $tablefield->get_field_setting( 'header_cell_content_type' );
$header_cell_content_type_editor = $tablefield->get_field_setting( 'header_cell_content_type_editor' );
$header_row_styles = $tablefield->get_field_setting( 'header_row_styles' );
$header_cell_styles = $tablefield->get_field_setting( 'header_cell_styles' );
// footer
$footer_row_content_type = $tablefield->get_field_setting( 'footer_row_content_type' );
$footer_row_content_type_editor = $tablefield->get_field_setting( 'footer_row_content_type_editor' );
$footer_cell_span = $tablefield->get_field_setting( 'footer_cell_span' );
$footer_cell_span_column = $tablefield->get_field_setting( 'footer_cell_span_column' );
$footer_cell_content_type = $tablefield->get_field_setting( 'footer_cell_content_type' );
$footer_cell_content_type_editor = $tablefield->get_field_setting( 'footer_cell_content_type_editor' );
$footer_row_styles = $tablefield->get_field_setting( 'footer_row_styles' );
$footer_cell_styles = $tablefield->get_field_setting( 'footer_cell_styles' );
Update
Update field
$tablefield->update_field();
Get Table
Get table
$tablefield->get_table();
Return
Returns the table HTML.