Modify existing plugin function with add_filter

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.