TransWikia.com

Modify existing plugin function with add_filter

WordPress Development Asked by odd_duck on December 10, 2021

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">

One Answer

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.

Answered by mozboz on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP