Skip to content

Instantly share code, notes, and snippets.

@rafaehlers
Created February 11, 2025 21:06
Show Gist options
  • Save rafaehlers/98003df7cff95847da4c9af06ec11d07 to your computer and use it in GitHub Desktop.
Save rafaehlers/98003df7cff95847da4c9af06ec11d07 to your computer and use it in GitHub Desktop.
DataTables > Buttons > Excel > Include URLs on export
document.addEventListener('DOMContentLoaded', function () {
wp.hooks.addFilter('gk.datatables.options', 'dt-custom-code', function (options) {
options.buttons = options.buttons.map(button => {
if ('excel' === button.extend) {
button.exportOptions = {
// stripHtml: false,
format: {
body: function (data, row, column, node) {
// Check if the cell data contains an anchor tag
if (data.indexOf('<a') !== -1) {
// Create a temporary element to parse the HTML
var el = document.createElement('div');
el.innerHTML = data;
var anchor = el.querySelector('a');
if (anchor) {
return anchor.href;
}
}
// Otherwise, return the data as is.
return data;
}
}
};
}
return button;
});
return options;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment