DataTables Excel Export - How to remove HTML tags from the export

Excel sheet with HTML tags in "Type of Incident" and "Date of Incident" columns

Excel table showing incident types with corresponding dates

By default, DataTables include HTML tags in an Excel export if you have links or text styles on your View columns. With the help of the code snippet below, you'll be able to remove those tags from your Excel export files.

document.addEventListener("DOMContentLoaded",()=>{wp.hooks.addFilter("gk.datatables.options","gv-strip-html-excel",t=>(t.buttons=t.buttons.map(t=>("excel"===t.extend&&(t.exportOptions=t.exportOptions||{},t.exportOptions.stripHtml=!0,t.exportOptions.format=t.exportOptions.format||{},t.exportOptions.format.body=(t,o,e,p)=>p?p.innerText:t.replace(/<[^>]*?>/g,"")),t)),t))});

This code has been minified to make it easy to copy. Here's a link to the full code.

This JavaScript code should be copied and pasted, as it is, on the Custom JavaScript editor that sits inside View Editor > View Settings metabox > Custom Code tab.

Custom JavaScript settings for removing HTML tags during DataTables Excel export

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us