Support » Plugin: TablePress » Print table name inside caption tag?

  • Resolved nimmolo

    (@nimmolo)


    Hi Tobias,

    Is there a way to filter the caption text so that the name or description of the table can be printed inside that <caption> tag (and also inside an <h2> element)? I’m trying the ‘tablepress_print_caption_text’ filter but it doesn’t seem to have the $table attributes available.

    Background: for layout and semantic HTML reasons, I’d prefer to print the table name (what usually appears in the <h2> tags) in the table <caption> element. I also would prefer to print this _above_ the table itself.

    This is partly because i have a margin offset for the entire <table> element and i’d prefer to keep the CSS simple, but it also seems from the spec of the <caption> element that the title of a table should go inside that element.

    • This topic was modified 2 years, 10 months ago by nimmolo.
    • This topic was modified 2 years, 10 months ago by nimmolo.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Also, this would help for screen reader accessibility reasons; especially on pages with multiple tables, the caption is read to screen reader users when they navigate into a table as the “label” for that table.

    Thread Starter nimmolo

    (@nimmolo)

    Oh!! I just found this, Tobias already made it. Thank you Tobias!
    I had not searching the right terms before:

    https://tablepress.org/extensions/table-caption-html-tag/

    This should help you @pbossley

    I modified Tobias’ plugin to use an <h2> tag inside the <caption>

    
    function tablepress_table_caption_html_tag( $caption, $table ) {
      $caption = preg_replace( '/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/', '&', $table['name'] );
      return '<h2 class="tablepress-table-name">' . $caption . '</h2>';
    }
    

    @tobiasbg maybe consider making this an option on the basic plugin? I feel like this is semantically a more correct use of the <caption> tag.

    • This reply was modified 2 years, 10 months ago by nimmolo.
    • This reply was modified 2 years, 10 months ago by nimmolo.
    • This reply was modified 2 years, 10 months ago by nimmolo.

    Thanks. Was all set to do something super hacky to make this work, glad I didn’t need to do that. FWIW I think this one is worthy of being put into the main plugin, though.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    Yes, making TablePress better for accessibility is already on my list!

    Good to hear that this solution helped!

    Best wishes,
    Tobias

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Print table name inside caption tag?’ is closed to new replies.