Extension 4: Changing DataTables strings

February 9th, 2010 by Tobias Leave a reply »

In this post of the WP-Table Reloaded Extensions series, I’ll show you a way to easily change the strings that the DataTables JavaScript library outputs around a table. This includes things like the “Search:”, “Showing i to j of n entries” and the other text strings.

To get started with WP-Table Reloaded Extensions, you should read the introduction and follow the included instructions. You will also find links to the other presented Extensions there.

Several people have approached me, asking how to change the “Search:” to “Filter:”, “Live Search:”, or other similar texts.
Those strings are all stored in a file that at the same time serves as the localization method for those strings, i.e. English and the other languages’ texts are simply stored in different files and only the needed one is loaded, depending on the locale that is used for WordPress. The files are all located in the “datatables” subfolder of the “languages” folder in the WP-Table Reloaded plugin folder.

Thus, changing strings is very similar to translating them to a new language: Find the file for your language in the mentioned folder and copy it to your theme’s folder (or another publicly available location on your webspace or server). This step will ensure that your changes will still be there after an update of the plugin. If you just change strings in the mentioned file, those changes will be gone after an update!

The file looks similar to this (this is the English file):

{
	"sProcessing":   "Processing...",
	"sLengthMenu":   "Show _MENU_ entries",
	"sZeroRecords":  "No matching records found",
	"sInfo":         "Showing _START_ to _END_ of _TOTAL_ entries",
	"sInfoEmpty":    "Showing 0 to 0 of 0 entries",
	"sInfoFiltered": "(filtered from _MAX_ total entries)",
	"sInfoPostFix":  "",
	"sSearch":       "Search:",
	"sUrl":          "",
	"oPaginate": {
		"sFirst":    "First",
		"sPrevious": "Previous",
		"sNext":     "Next",
		"sLast":     "Last"
	}
}

You can change any string that is on the right side of a colon (:). Make sure to not remove any of the quotation marks. Also, you can not use quotation marks in your changed strings! The words that are surrounded by an underscore (_) are placeholders for the numbers or input fields that are automatically added by the DataTables script. You must not alter those (you can however put them in a new place within the string).

This is an example of a file with changed string:

{
	"sProcessing":   "Please wait...",
	"sLengthMenu":   "Show _MENU_ rows",
	"sZeroRecords":  "No matching rows found in the table",
	"sInfo":         "Showing _START_ to _END_ of _TOTAL_ rows",
	"sInfoEmpty":    "Showing 0 to 0 of 0 rows",
	"sInfoFiltered": "(found from _MAX_ total entries)",
	"sInfoPostFix":  "",
	"sSearch":       "Live Filter:",
	"sUrl":          "",
	"oPaginate": {
		"sFirst":    "First",
		"sPrevious": "Previous",
		"sNext":     "Next",
		"sLast":     "Last"
	}
}

After making your changes, again make sure to have the file stored in your theme’s folder. You might want to use a more recognizable filename, like “datatables.txt” which I will use here.

It will then have a URL like http://www.example.com/wp-content/your-theme/datatables.txt. (You will need this URL, adjusted to your site, in the next step.)

After creating the file with the changed strings, we need to tell WP-Table Reloaded (i.e. the DataTables library) to use the file for obtaining the strings:

/**
 * Changes the file used for the DataTables strings to a different file (i.e. with changed strings)
 * @author Tobias Baethge
 * @see https://tobias.baethge.com/2010/02/extension-4-changing-datatables-strings/
 */
function wp_table_reloaded_change_datatables_language_file_url( $language_file_url ) {
    return 'http://www.example.com/wp-content/your-theme/datatables.txt'; // change this to the corresponding URL on your server
}
add_filter( 'wp_table_reloaded_url_datatables_language_file', 'wp_table_reloaded_change_datatables_language_file_url' );

Just replace the URL in this function with the URL to your file and the strings of the DataTables library will be taken from the file.

The code form above just needs to be copied into the file “wp-table-reloaded-extensions.php”, created according to these instructions (after the Plugin Header comment, but before the closing PHP bracket ?>). Then, just activate the new plugin “WP-Table Reloaded Extensions”, if you haven’t. That’s it! :-)

If you like this series of Extensions, I’m happy about any feedback, and especially about further suggestions!

Previous posts in this series that might interest you: