1. Home
  2. Knowledge Base
  3. Posts Table Pro
  4. Advanced Usage
  5. Developer documentation

Developer documentation

There are a number of filters provided in Posts Table Pro which allow you to customise the behaviour of the plugin.

Please note that this code is aimed at developers and if you don’t know how to use it then you should ask your developer. If you don’t have one, we recommend posting a job on Codeable. Alternatively, please contact us to request a quote from our customizations service.

posts_table_supported_languages

Allows you to add additional languages into the plugin which are not currently supported. Note that you must use the correct locale for your language as the key in the $langs array. In this example we’re adding Croatian so the locale is ‘hr_HR’.

add_filter( 'posts_table_supported_languages', 'ptp_supported_languages' );
function ptp_supported_languages( $langs ) {
    $langs['hr_HR'] = 'http://example.com/wp-content/languages/plugins/Croatian.json';
    return $langs;
}

posts_table_language_defaults

This filter allows you to override the default values for various items of text used within the plugin. You might use this if you want to change the text for a particular item (e.g. the search box label) which has been provided by the plugin in your chosen language.

The example below changes the search box label to ‘Filter’, and also the empty table text.

function ptp_set_language_defaults( $defaults ) {
    $defaults['search'] = 'Filter';
    $defaults['emptyTable'] = 'Sorry, no articles currently available.';
    return $defaults;
}
add_filter( 'posts_table_language_defaults', 'ptp_set_language_defaults' );

The full list of language strings and their defaults are:

Option Default
$defaults[‘info’] Showing _START_ to _END_ of _TOTAL_ entries
$defaults[‘info’] Showing _START_ to _END_ of _TOTAL_ entries
$defaults[‘infoEmpty’] Showing 0 entries
$defaults[‘infoFiltered’] (_MAX_ entries in total)
$defaults[‘lengthMenu’] ‘Show _MENU_ entries
$defaults[’emptyTable’] No data available in table.
$defaults[‘zeroRecords’] No matching records found.
$defaults[‘search’] Search:
$defaults[‘searchPlaceholder’]
$defaults[‘paginate’][‘first’] First
$defaults[‘paginate’][‘last’] Last
$defaults[‘paginate’][‘next’] Next
$defaults[‘paginate’][‘previous’] Previous
$defaults[‘filterBy’] Filter:
$defaults[‘resetButton’] Reset

posts_table_filter_label

Allows you to change the “Filter:” label shown next to the search filters (if enabled). The posts_table_language_defaults filter will take precedence, if both are used.

add_filter( 'posts_table_filter_label', 'ptp_custom_filter_label' );
function ptp_custom_filter_label( $label ) {
    return "Search by:";
}

posts_table_reset_button

Allows you to change the text used for the Reset button link above the table. The posts_table_language_defaults filter will take precedence, if both are used.

add_filter( 'posts_table_reset_label', 'ptp_custom_reset_button' );
function ptp_custom_reset_button( $label ) {
    return "Clear";
}

posts_table_search_label

Allows you to modify the search label shown next to the search box above the table. Defaults to “Search:”. The posts_table_language_defaults filter will take precedence, if both are used.

add_filter( 'posts_table_search_label', 'ptp_custom_search_label' );
function ptp_custom_search_label( $label ) {
    return "I'm Looking for:";
}

posts_table_inline_config

Filters which allows you to override the inline table configuration used when the DataTables script is initialised. You can use this filter, for example, to disable or enable certain table features. The following example disables the ordering/sorting feature in all product tables:

// Disable column sorting
add_filter( 'posts_table_inline_config', 'ptp_inline_table_config', 10, 2 );
function ptp_inline_table_config( $config, $posts_table ) {
    $config['ordering'] = false;
    return $config;
}

posts_table_process_shortcodes

By default, Posts Table Pro will strip shortcodes from the post content so any content that would appear inside these shortcodes will not appear in the table. You can enable shortcodes on a per-table basis using the shortcodes option, but if you want to enable them globally, return true from this filter as follows:

add_filter( 'posts_table_process_shortcodes', '__return_true' );

Whether to run the posts table shortcode on the search results page. Defaults to false. Normally, you would want to prevent the shortcode running within the search results, as only a snippet or excerpt of each page or post is shown in the search results. However, if you want to enable posts tables in the search results, set this filter to return true:

add_filter( 'posts_table_run_in_search', '__return_true' );

posts_table_data_<column>

There are numerous filters which allow you to modify the data added to the table. One is provided for each column in the table in the form posts_table_data_<column>. So to filter the image column you would add a filter called posts_table_data_image. To filter custom taxonomies use the filter posts_table_data_terms.

add_filter( 'posts_table_data_image', 'ptp_custom_data_image' );
function ptp_custom_data_image( $image ) {
    // do something with $image
    return $image;
}

posts_table_data_custom_field

Filters which allows you to override the value of a custom field displayed in the table. It accepts 3 arguments – the custom field value, the custom field key and the current post object.

// Override the 'extra_notes' custom field
add_filter( 'posts_table_data_custom_field', 'ptp_set_custom_field', 10, 3 );
function ptp_set_custom_field( $value, $field, $post ) {
    if ( 'extra_notes' === $field ) {
       // do something with $value
    }
    return $value;
}

posts_table_acf_value

Filters which allows you to override the custom field value returned from Advanced Custom Fields. It accepts 3 arguments – the custom field value, the ACF field object and the current post ID.

add_filter( 'posts_table_acf_value', 'ptp_override_acf_value', 10, 3 );
function ptp_override_acf_value( $value, $field_obj, $post_id ) {
    if ( 'file' === $field_obj['type'] ) {
       // do something with $value
    }
    return $value;
}

posts_table_custom_data_<column>

Filter which allows you to add custom data to the table, in addition to the standard columns provided by Posts Table Pro. It accepts two arguments: the custom data to return (defaults to an empty string) and the current post object.

For example, to add a custom column “rating” to your table, first add the custom column to your shortcode:
[posts_table columns="title,content,date,rating"]

Then add a filter to your theme or plugin to return this data:

add_filter( 'posts_table_custom_data_rating', 'ptp_add_custom_data_rating', 10, 2 );
function ptp_add_custom_data_rating( $value, $post ) {
    $value = 'xyz'; // set value as required
    return $value;
}

posts_table_cell_data_<column>

Filter which allows you to modify the HTML data for a cell before it is added to the table. The <column> in the filter should be replaced with the column you wish to filter. For example, the filter data in the title column:

add_filter( 'posts_table_cell_data_title', 'ptp_custom_title_html', 10, 2 );
function ptp_custom_title_html( $title, $post ) {
    return '<span class="xyz">' . $title . '</span>';
}

posts_table_custom_class

Use this filter to add additional classes to the posts table’s <table> tag. E.g.

add_filter( 'posts_table_custom_class', 'ptp_add_custom_class', 10, 2 );
function ptp_add_custom_class( $class, $posts_table ) {
    return 'featured-table';
}

posts_table_row_class

Use this filter to add extra CSS classes to each row in the table, or selectively based on the current post.

add_filter( 'posts_table_row_class', 'ptp_custom_row_class', 10, 2 ); 
function ptp_custom_row_class( $classes, $post ) {
    if ( 23 === $post->ID ) {
        $classes[] = 'featured';
    }
    return $classes;
}

posts_table_column_class_<column>

This filter allows you to add additional column classes to your table. Replace <column> with the column that you want to add a class to. You may add additional classes, but do not remove any from the list passed in, as it may affect the operation of the table.

add_filter( 'posts_table_column_class_title', 'ptp_custom_title_column_class' );
function ptp_custom_title_column_class( $classes ) {
    $classes[] = 'highlight';
    return $classes;
}

posts_table_column_heading_<column>

Filter which allows you to override a column heading for all posts tables on your site. If a custom heading is specified within the shortcode itself (in the columns option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_heading_title', 'ptp_custom_heading_title' );
function ptp_custom_heading_title( $heading ) {
    return 'My Title';
}

posts_table_column_priority_<column>

Filter which allows you to override a column priority for all posts tables on your site. If a priority is specified within the shortcode itself (using the priorities option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_priority_date', 'ptp_custom_priority_date' );
function ptp_custom_priority_date( $priority ) {
    return 10;
}

posts_table_column_width_<column>

Filter which allows you to override a column width for all posts tables on your site. If a width is specified within the shortcode itself (using the widths option), then it will take priority over the value returned from this filter.

add_filter( 'posts_table_column_width_author', 'ptp_custom_width_author' );
function ptp_custom_width_author( $width ) {
    return '100px';
}

posts_table_column_searchable_<column>

This filter allows you to set whether or not a column is “searchable” in the posts table. If a column is searchable, it means that the column will be used when the user types something into the search box above the table.

By default, the plugin includes all columns for searching, except for the image column. To override this behaviour, use this filter with the appropriate column name added to the filter.

E.g. to exclude the title column from the table search:

add_filter( 'posts_table_column_searchable_title', '__return_false' );

If you wish to disable searching for a custom field or taxonomy, use the column name without the cf: or tax: prefix:

add_filter( 'posts_table_column_searchable_my_custom_field', '__return_false );

posts_table_column_sortable_<column>

This filter allows you to set whether or not a column is “sortable” in the posts table. If a column is sortable, the table can be re-ordered by that column using the up/down arrows that appear in the column heading.

If you’re using lazy load, the sorting is restricted to certain columns only. If you’re not using lazy load, then sorting is enabled for all columns except image.

E.g. to prevent the table being sorted by title:

add_filter( 'posts_table_column_sortable_title', '__return_false' );

See the note above about using this filter for custom field or taxonomy columns.

posts_table_max_posts_limit

Filter which allows you to override the maximum posts limit (500 posts) set in Posts Table Pro. Note that this option only applies when you are not using the lazy_load option.

add_filter( 'posts_table_max_posts_limit', 'ptp_set_posts_limit', 10, 2 );
function ptp_set_posts_limit( $limit, $posts_table ) {
    return 1000;
}

posts_table_query_args

Filter which allows you to override the query args passed to WP_Query prior to retrieving posts from the database. See WP_Query documentation for details.

add_filter( 'posts_table_query_args', 'ptp_custom_query_args', 10, 2 );
function ptp_custom_query_args( $args, $posts_table ) {
    // do something with $args
    return $args;
}

posts_table_shortcode_output

Allows you to modify the HTML returned by the [posts_table] shortcode. Takes two parameters: the HTML and the Posts_Data_Table instance.

add_filter( 'posts_table_shortcode_output', 'ptp_shortcode_output', 10, 2 );
function ptp_shortcode_output( $html, $table ) {
    // do something with HTML
    return $html;
}

 

Was this article helpful?

Related Articles