Skip to content

Instantly share code, notes, and snippets.

@amberhinds
Created August 14, 2023 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amberhinds/16f238a53d8e72b1a114af174a6c2fde to your computer and use it in GitHub Desktop.
Save amberhinds/16f238a53d8e72b1a114af174a6c2fde to your computer and use it in GitHub Desktop.
Add row and col scope to table headers in TablePress
/**
* Add scope attributes to table headers
*
* @param mixed $output HTML output of the table.
* @param mixed $table Table object.
* @param array $render_options Render options.
* @return string HTML output of the table.
*/
function tablepress_add_scope( $output, $table, $render_options ) {
$dom = new DOMDocument();
$dom->loadHTML( $output );
$xpath = new DOMXPath( $dom );
if ( $render_options['table_head'] ) {
$th = $xpath->query( '//thead/tr/th' );
foreach ( $th as $node ) {
$node->setAttribute( 'scope', 'col' );
}
}
if ( $render_options['first_column_th'] ) {
$th = $xpath->query( '//tbody/tr/th' );
foreach ( $th as $node ) {
$node->setAttribute( 'scope', 'row' );
}
}
$output = $dom->saveHTML();
return $output;
}
add_filter( 'tablepress_table_output', 'tablepress_add_scope', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment