Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created May 21, 2025 23:04
Show Gist options
  • Save rafaehlers/6fcc2c4deecf0fea201ace93bb41d287 to your computer and use it in GitHub Desktop.
Save rafaehlers/6fcc2c4deecf0fea201ace93bb41d287 to your computer and use it in GitHub Desktop.
DataTables > Buttons > Excel > Exclude HTML tags on export
document.addEventListener('DOMContentLoaded', () => {
wp.hooks.addFilter('gk.datatables.options', 'gv-strip-html-excel', (opts) => {
opts.buttons = opts.buttons.map((btn) => {
if (btn.extend === 'excel') {
// Ensure the sub-objects exist
btn.exportOptions = btn.exportOptions || {};
btn.exportOptions.stripHtml = true; // works on modern Buttons builds
// Fallback: manual stripping for older builds
btn.exportOptions.format = btn.exportOptions.format || {};
btn.exportOptions.format.body = (data, row, col, node) =>
node ? node.innerText // node.innerText is already tag-free
: data.replace(/<[^>]*?>/g, ''); // just in case
}
return btn;
});
return opts;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment