Created
August 14, 2023 19:07
-
-
Save amberhinds/16f238a53d8e72b1a114af174a6c2fde to your computer and use it in GitHub Desktop.
Add row and col scope to table headers in TablePress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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