0

I am relatively new to Wordpress and using custom filters and hooks. My site uses the TablePress plugin. But I need to be able to add the html attribute of

role=presentation

To all tables created through the plugin. The only way to do so is to

There’s not direct way to add this, but you could e.g. use the tablepress_table_tag_attributes filter hook that can be used to change the attributes of the element. The filter is defined in class-render.php

as per https://wordpress.org/support/topic/table-role/#post-9665599

The class-render.php mentioned is below:

$table_attributes = apply_filters( 'tablepress_table_tag_attributes', $table_attributes, $this->table, $this->render_options );
$table_attributes = $this->_attributes_array_to_string( $table_attributes );

https://github.com/TobiasBg/TablePress/blob/master/classes/class-render.php#L519

In my functions.php file I have added:

add_filter( 'tablepress_table_tag_attributes' , 'add_presentation_role', 11);
function add_presentation_role() {
    // return ?
}

But i am struggling to see what I need to put inside my function. Anything I put in breaks my website or results in a blank page. To confirm I would like to add the the role attribute so my tables update from

<table id="tablepress-{ID}" class="tablepress tablepress-id-{ID}">

to

<table id="tablepress-{ID}" class="tablepress tablepress-id-{ID}" role="presentation">
  • Filters always take in an input, and return a modified output, but your filter does neither. It doesn't accept a parameter that can be "filtered", so you have nothing to modify then return. Keep in mind that we can help you understand how filters work, but we can't help you with Tablepress. Tablepress is a 3rd party plugin, but 3rd party plugins are offtopic on this site – Tom J Nowell Jul 23 at 11:58
  • Thank you @TomJNowell. Apologies. Does that mean that effectively what I am trying to do is not possible? Is it possible to pass through the $table_attributes from the plugin into my own add_presentation_role function? – odd_duck Jul 23 at 12:04
  • Re-read, the problem here is a fundamental misunderstanding of how a filter works. You can do what you wanted to do, but the code you've misunderstood how filters work and what they are. I strongly recommend looking up one of the questions on the site about how filters work, reading about the basics will make clear what you did wrong, although I said as much in my previous comment. Filters always take a function parameter, and always return something. Your example does neither. – Tom J Nowell Jul 23 at 12:10
1

So you're half way there with the code you have, you're only missing a small part which is that your filter function takes information about what it's filtering through function parameters, can then make changes to the thing that's being filtered (or not) and then must pass that back out (return it). I.e. In this case if for some reason your code decided that you didn't want to change $table_attributes you would still need to return the one that you got passed in.

So if this is how the filter is run:

$table_attributes = apply_filters( 'tablepress_table_tag_attributes', $table_attributes, $this->table, $this->render_options );

It means that your filter function should look minimally like this:

function add_presentation_role($table_attributes, $table, $render_options) {
    // do some stuff to $table_attributes
    return $table_attributes;
}

add_filter( 'tablepress_table_tag_attributes' , 'add_presentation_role', 11, 3);

Note:

  • The parameters you get into your custom function match those that the filter is called with. You don't have to use them, but they're available if you need any of that information.
  • The number on the end of the add_filter call is how many parameters your function takes.
  • You don't have to take all of the parameters if you're not using them, but it doesn't hurt and imo it's nice to have your filter function match how the filter is called

So I haven't looked at the format of the $table_attributes variable, but it's likely that all you need to do is add the extra attributes to that and return it inside your filter. You need to refer to the docs or read the code of this plugin to see how that variable is structured. (Probably it's an associative array so you can do $table_attributes['new_attribute_name'] = "new_attribute_value", but that's a guess and you should confirm it)

Hope that helps, please post here if you need more info.

| improve this answer | |
  • Thank you so much @mozboz. This works perfectly. – odd_duck Jul 23 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.