Select plugin version:
Since version: v1.8.0
Cell formatting
The cell formatting lets editors align the content of cells and change how its text looks, without writing any HTML. You can set it for the whole table, for the header, the body, the footer or the stub column, and for single columns, rows and cells. The more specific setting wins.
Editors only get the formatting after you switch it on in the field settings, see Switch it on.
The plugin outputs the formatting as a data attribute on the cells and ships a small stylesheet for it. Themes can use the attribute for their own styles.
Switch it on
The formatting is off by default, for new fields and for existing fields after the update. In the settings of the table field in your field group, you decide which formatting options editors get. Each level (the table, the columns, the rows and cells of the body, and the rows and cells of the header and footer) has its own checkboxes:
- Formatting shows the Formatting tab on that level.
- Vertical Alignment, Horizontal Alignment, Font Weight, Italic, Text Decoration, Text Transform, Font Size, Text Wrapping and Numbers show the options of the tab.
- In the checkboxes of Settings for the table, each of the tabs Table Header, Table Body, Table Footer and Stub Column is followed by its own Formatting checkbox with the same nine options.
If you uncheck an option, it is only hidden in the editor. Values that are already stored, for example from the default table, the Table Manager or the REST API, still apply to the output. See Setup editing options.
What you can format
| Option | Row | Values |
|---|---|---|
| Horizontal alignment | Alignment | Left, Center, Right, Justify |
| Vertical alignment | Alignment | Top, Middle, Bottom |
| Font weight | Font | The weights of the theme font, for example 400 and 700, or Bold and Normal |
| Italic | Font | Italic, Normal |
| Font size | Text |
Small, Medium, Large, Extra large. Developers can change the list with the filter acf_tablefield/formatting/font_sizes.
|
| Transform | Text | Uppercase, Lowercase, Capitalize, None |
| Decoration | Text | Underline, Line through, None |
| Numbers | Text | Tabular, Proportional |
| Wrapping | Text | Wrap, No wrap |
Every option also has the value Theme. Below the table level, an option that is not set inherits its value, see Inherit and Theme.
Set the formatting
For the table, a column, a row or a cell
- Open the settings of the level you want to change: the Settings for the table button, or the menu icon of a column, row or cell.
- Open the Formatting tab. The options are grouped in the rows Alignment, Font and Text. Each option is a button that shows the value that applies.
- Click the button of an option, for example Font weight. A small panel opens with the values of the option.
- Pick a value. The panel closes.
- Click Apply Changes.
The table shows the formatting at once.
For the header, the body, the footer or the stub column
- Open the Settings for the table.
- Open the tab Table Header, Table Body, Table Footer or Stub Column.
- Set the options in the field group Formatting, the same way as above.
- Click Apply Changes.
The header and the footer only show their formatting when they are switched On, see Header and footer. The same applies to the stub column.
Good to know
- Picking the value that is already set removes it again. On the table level, the option goes back to Theme. Below the table level, it inherits again. The link ↺ Inherit in the panel does the same.
- A reset button at the end of each row removes all values of that row on this level. Reset all in the legend below the options removes all of them. The buttons only show up when something is set.
- The keyboard works too: Enter, Space or the down arrow opens the panel, the arrow keys move between the values, Escape closes it.
- Until you click Apply Changes, you can still leave with Discard Changes and Close.
Inherit and Theme
Every option is resolved on its own. The levels are checked in this order, and the first one with a value wins:
Cell → Row → Column → Stub column → Header, body or footer → Table
- Inherit means that nothing is set on this level. The value comes from the next level in the order.
- Theme on the table level is the default: nothing is set, and your theme decides how the cells look.
- Theme below the table level is a value of its own. It stops the search, so a cell, row or column looks like the theme again, even when a higher level sets something else. The plugin outputs nothing for it.
The panel of an option tells you where the value comes from, for example Center inherited from Column 2, Overrides 600 from Table or Uses the Theme setting, nothing set above. On the table level it says Passed on to columns, rows and cells.
The buttons show the state of each option. The legend below the options explains them:
- Dark fill: the value is set on this level (Set on this cell, Set on this row, Set for Head …).
- Black frame: the value is inherited from a higher level (Inherited).
- Thin grey frame: nothing is set here or above, the theme decides (Theme).
Why the row comes before the column
Rows usually carry exceptions, like a bold totals row or a highlighted row. Columns usually carry rules, like numbers aligned right. The exception wins. A column value reaches a cell only where the row does not set that option. In the same way, at the corner where the header meets the stub column, the header comes first.
Because of this, the settings of a row don't show the values of the columns as inherited, and the settings of a column don't show the values of the rows. A row crosses all columns, and each column can have a different value. Row and column values only meet in the cell, and the settings of a cell show the result. The header, body, footer and stub column panels only inherit from the table level.
Stub column and spanned cells
- The formatting of the stub column only applies while the stub column is switched on. When you switch it off, the values are kept and apply again when you switch it back on.
- The stub column is always the first column. If you move columns, its formatting applies to the first column, whatever it contains.
- A spanned cell takes the row and column formatting of its start cell: the top row and the first column it covers.
An example
- The table is set to Horizontal alignment: Left. All cells are aligned left.
- The Table Header is set to Horizontal alignment: Center. All header cells are centered, because the header comes before the table.
- The third column is set to Horizontal alignment: Right. Its body cells and its header cell are aligned right, because the column comes before the header.
- The last row of the body, a totals row, is set to Horizontal alignment: Center and Font weight: Bold. All its cells are centered and bold, also the cell in the third column, because the row comes before the column.
- One cell in the totals row is set to Horizontal alignment: Theme. That cell is aligned the way the theme does it. It stays bold, because every option is resolved on its own.
Left and right in right-to-left languages
The horizontal alignment is stored as start and end of the text, not as left and right. In left-to-right languages, start is left and end is right, and the editor shows the values as Left and Right. In a right-to-left admin, for example in Arabic or Hebrew, the buttons are mirrored.
On the website, start and end follow the direction of the language. A translated copy of a table in a right-to-left language, for example with Polylang, is mirrored by itself. You don't have to change the alignment.
Font weights and font sizes
Font weight offers the weights that the font of the table has in your theme, as numbers like 400 or 700. The plugin reads them from the theme.json or the font library, from the font faces of the font family that the table uses. The numbers are shown in the font of the admin, not in the theme font. If the weights are not known, the editor offers Bold and Normal. Developers can change the list with the filter acf_tablefield/formatting/font_weights.
Font size has the sizes Small (0.875em), Medium (1em), Large (1.25em) and Extra large (1.5em). They are relative to the text size of the table, and they never add up: Large on the table and Large on a cell is still Large. Medium sets 1em on purpose, which is not the same as Theme. Developers can change the list with the filter acf_tablefield/formatting/font_sizes. If a size is removed from the list, cells that use it show Unknown size (…), no effect in the editor and keep the size of the theme.
Text wrapping and numbers
Use No wrap only for short content, like dates, prices or codes. Text in such a cell never wraps, so a long text can make the table wider than the screen and force horizontal scrolling on phones. Developers can set a minimum width with the filter acf_tablefield/formatting/nowrap_min_width: below it, the cells wrap again. With a minimum width, the table is output inside an extra div, which can affect theme CSS that targets the table directly.
Numbers: Tabular gives all digits the same width, so numbers in a column line up. This only works if the font has tabular figures. Many fonts have them, but not all.
Accessibility
Line through is only a visual style. Screen readers don't announce it. If the strikethrough means something, like an old price or a cancelled date, put it in the content with <s> or <del> and add text for screen readers:
An old price in the cell content
<s><span class="screen-reader-text">Old price: </span>49 €</s> 39 €
For capital letters, write the text normally and use Transform: Uppercase. Screen readers read the text normally instead of spelling it, and you can change the case later without typing the text again.
Cell formatting and formatting in the text
The cell formatting applies to the whole cell. Formatting inside the content, for example from the editor toolbar, wins over it:
- Bold text (
<strong>) stays bold, also when the cell has Font weight: Normal or 400. - Underlined links and
<u>stay underlined, also when the cell has Decoration: None. CSS can't remove the decoration of an element inside the cell. - Transform changes the whole content of the cell, also the text of links.
What happens when you change the table
- Add a row or a column: it starts without formatting of its own and inherits from the levels above, for example a new row gets the formatting of the columns, the body and the table. It does not copy the formatting of its neighbours.
- Move a row, column or cell: its formatting moves with it.
- Remove a row or column: its formatting is removed too.
- Reset the table: the formatting goes back to the formatting of the default table.
- Import a CSV file: new rows and cells come without formatting. With Overwrite, the formatting of the table level is kept, including the header, body, footer and stub column, and the formatting of columns, rows and cells is removed. With Append or Prepend, all formatting is kept.
- Backups and restores keep the formatting as it is.
- Translations with Polylang and DeepL copy the formatting as it is and translate only the content.
- Switching off the header, footer or stub column keeps their formatting. It applies again when you switch them back on.
Default table
The formatting is part of the table data. Set it in the default table like in any table, and every new table starts with it. If you then hide the formatting options from editors in the field settings, the formatting of the default table still applies, and editors can’t change the design.
Output on the website
When the table is output with get_table() or the shortcode, a formatted td or th gets the attribute data-acftf-format. It holds one token for each option that applies to the cell, separated by spaces. Theme outputs no token. A cell without formatting gets no attribute.
| Option | Tokens |
|---|---|
| Vertical alignment |
va-top va-middle va-bottom
|
| Horizontal alignment |
ha-start ha-center ha-end ha-justify
|
| Font weight |
fw-100 to fw-900, Bold is fw-700, Normal is fw-400
|
| Italic |
fs-italic fs-normal
|
| Decoration |
td-underline td-line-through td-none
|
| Transform |
tt-uppercase tt-lowercase tt-capitalize tt-none
|
| Font size |
fz- and the key of the size: fz-s fz-m fz-l fz-xl
|
| Wrapping |
ws-wrap ws-nowrap
|
| Numbers |
nv-tabular nv-proportional
|
A formatted cell
<td data-acftf-format="va-middle ha-center fw-700">Total</td>
Output in feeds, emails and PDFs
Feeds, emails and PDF generators usually don't load the stylesheet of the plugin. In feeds, for example for RSS newsletters and feed readers, every formatted cell gets the formatting as a style attribute as well. For emails, PDFs or other output without the stylesheet, turn this on with the filter acf_tablefield/formatting/inline_styles. The attribute data-acftf-format stays in the output.
With inline styles, No wrap always applies, the minimum width of the filter nowrap_min_width does not. Some feed readers and email programs remove styles anyway.
Inline styles for one table
add_filter( 'acf_tablefield/formatting/inline_styles', '__return_true' );
$html = get_table( 'my_table', $post_id );
remove_filter( 'acf_tablefield/formatting/inline_styles', '__return_true' );
The result is a cell like <td data-acftf-format="fw-700" style="font-weight: 700">.
Stylesheet, specificity and loading
The plugin styles the tokens with the stylesheet formatting.css. Every rule uses the data attribute twice, for example table td[data-acftf-format][data-acftf-format~="fw-700"]. This gives the rules a specificity of (0,2,2). That is higher than common theme rules like .entry-content th (0,1,1), .entry-content table th (0,1,2) or .wp-block-table td (0,1,1), so the formatting wins without !important. Theme rules with IDs or !important are not overridden on purpose.
On the website, the stylesheet is only loaded when a table on the page has formatted cells. It is enqueued while the table is rendered, so WordPress prints it in the footer. Since WordPress 7.1, WordPress moves such late styles into the head, after the theme styles. In both cases the stylesheet comes after the theme styles, so it wins on equal specificity.
In the WordPress admin and in the block editor, the stylesheet is always loaded. The table editor sets the same attribute on its cells.
Customize it in your theme
There are three ways to adapt the formatting to your theme:
- Write your own rules for the tokens. The next example does this.
- Turn off the plugin stylesheet and style the tokens yourself. See the filter
acf_tablefield/formatting/enqueue_style. The attribute stays in the output. The CSS that the plugin generates for custom font sizes and for the minimum width of No wrap is turned off with it. - Change the tokens with the filters
acf_tablefield/formatting/cell_tokens(PHP) andformatting/cell_tokens(JavaScript). The PHP filter changes the output on the website, the JavaScript filter changes the table editor. For the same look in both, set both filters the same way.
HTML of the table output
<table class="my-table">
<tbody>
<tr>
<td data-acftf-format="ha-center fw-700">Total</td>
</tr>
</tbody>
</table>
Your own rule for fw-700
table.my-table td[data-acftf-format][data-acftf-format~="fw-700"],
table.my-table th[data-acftf-format][data-acftf-format~="fw-700"] {
font-weight: 800;
color: #1d4ed8;
}
This rule gives bold cells a heavier weight and a color. The plugin rule has the specificity (0,2,2). A selector with only one attribute, like .my-table td[data-acftf-format~="fw-700"], has (0,2,1) and loses. This rule has (0,3,2): the class, both attributes, and the elements table and td.
Headless
In the formatted value, from get_field() or from the REST API with acf_format=standard, every cell has a format object with the formatting that applies to it, inheritance already resolved. See the REST API. In GraphQL, the format object is only there for table fields inside a repeater, a flexible content or a clone, as part of the JSON string.
Related
- Table data: the options
fm.va,fm.ha,fm.fwand the others, and their stored values, are described in the field data. - Formatting in PHP: the Table Manager reads and changes the formatting with names like
font_weightand values like700. - Own areas: developers can add formatting areas, for example for the last column, with the filter
acf_tablefield/formatting/areas. Own areas have no panel in the editor. Set their values with the Table Manager or in the table data. - Styles: to add your own CSS classes to cells, see Styles.