Viewing 9 replies - 16 through 24 (of 24 total)
  • Thanks @tobiasbg

    I’ve been working on a hook to add the scope attribute, but I was wondering how I can check for the shortcode attribute (or render_option) first_column_th. Here is what I have so far:

    
    add_filter( 'tablepress_cell_tag_attributes', 'custom_cell_tag_attributes', 10, 7 );
    function custom_cell_tag_attributes( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    	if( $col_idx === 1 ) {
    		$tag_attributes['scope'] = 'row';
    	}
    	
    	return $tag_attributes;
    }
    

    This puts a scope="row" attribute to the first tag in each row, but I would only like to do this if the first tag is a header tag <th>.

    Any ideas?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    you can’t really check inside that function so that the best option would be to check in a different hook that is executed earlier (e.g. tablepress_table_render_options) and only add your filter then. Then, remove the filter again in e.g. tablepress_table_output to not influence (potential) other tables on the same page.

    Regards,
    Tobias

    Now we’re getting a little over my head. Can I combine hooks into one function to use pass in other variables into a single function? Here’s what I mean:

    
    add_filter( 'tablepress_table_render_options', 'custom_cell_tag_attributes', 10, 2 );
    add_filter( 'tablepress_cell_tag_attributes', 'custom_cell_tag_attributes', 10, 7 );
    function custom_cell_tag_attributes() {
       ...
    }
    

    If this is possible, could I just check if $render_option['first_column_th'] === 'true'?

    Does this make sense?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    no, this is not possible like that, as the filters filter different variables.

    In the first one, you are given the $render_options array (and have to return it).
    With that, check for the first_column_th and only then execute the add_filter() for the second filter hook.
    For the tablepress_table_output hook, you can do it in the same way, except using remove_filter().

    Regards,
    Tobias

    The following code adds the scope="row" attribute to the row header tag, but, as you mentioned, this will apply this to all tables on the page:

    add_filter( 'tablepress_table_render_options', 'check_table_render_options', 10, 2 );
    function check_table_render_options( $render_options, $table ) {
    
    	if( $render_options['first_column_th'] === true ) {
    		add_filter( 'tablepress_cell_tag_attributes', 'custom_cell_tag_attributes', 10, 7 );
    		function custom_cell_tag_attributes( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    			if( $col_idx === 1 ) {
    				$tag_attributes['scope'] = 'row';
    			}
    			
    			return $tag_attributes;
    		}
    	}
    	
    	return $render_options;
    
    }

    I’m still trying to wrap my head around what to do with the tablepress_table_output filter.

    @tobiasbg I decided to add an else statement to the original filter without modifying the tablepress_table_output filter.

    This solution seems to work:

    add_filter( 'tablepress_table_render_options', 'check_table_render_options', 10, 2 );
    function check_table_render_options( $render_options, $table ) {
    
    	if( $render_options['first_column_th'] === true ) {
    		add_filter( 'tablepress_cell_tag_attributes', 'custom_cell_tag_attributes', 10, 7 );
    		function custom_cell_tag_attributes( $tag_attributes, $table_id, $cell_content, $row_idx, $col_idx, $colspan_row, $rowspan_col ) {
    			if( $col_idx === 1 ) {
    				$tag_attributes['scope'] = 'row';
    			}
    			
    			return $tag_attributes;
    		}
    	} else {
    		remove_filter( 'tablepress_cell_tag_attributes', 'custom_cell_tag_attributes' );
    	}
    	
    	return $render_options;
    
    }

    Do you see any errors here?

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    yes, this should indeed also work. ??

    Regards,
    Tobias

    Great. Thanks for your help with this.

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 9 replies - 16 through 24 (of 24 total)
  • The topic ‘Format first column as a header’ is closed to new replies.