How to migrate a table to Custom Post Types keeping the HTML content

When you try to migrate the content from a table to Custom Post Types, you may lose the links, headings, bolds, italics, and other tags and format if you just try to copy the table to Excel or Google Sheets, and then import that CSV using WP All Import. In this tutorial, you will find the trick to prevent this issue, and move all the HTML from that table to your WYSIWYG custom fields.

This tutorial assumes that you have already:

  1. Registered your Custom Post Type. You can use the Custom Post Type UI plugin for this.
  2. Registered your Custom Fields using the Advanced Custom Fields plugin.

Also, install and activate the following plugins before you start:

Now that you are all set, let’s start!

  1. Go to the page that is displaying your table and open your browser console.
  2. Paste the following code in your console, and press Enter to run it:
				
					( function( $ ) {
    $( 'tbody tr' ).each( function() {
        $( this ).find( 'td' ).each( function() {
            let str = String( $( this ).html() ).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
            $( this ).html( str );
        });
    });
})( jQuery );
				
			
  1. Select all the table cells with your mouse and copy / paste it into a spreadsheet (Excel, Google Sheets or your preferred). It’s not needed but recommended to:
    1. Change the headings on the first row of the spreadsheet to a format like_this_instead_of_using_regular_text
    2. Add a first column named ID to use it as “Unique Identifier” on WP All Import.
      WP All Import will associate imported records from this file on different import sessions based on the Unique Identifier. This way it can detect if the record has been modified, removed, or it’s a new record.
       
  2. Export the spreadsheet as CSV.
  3. Finally, just create a regular import using WP All Import. But instead of mapping the CSV columns with the Custom Fields section as you regularly do with the Drag&Drop feature when setting the “template”, use the new Advanced Custom Fields Add-On section that will appear because you previously installed the ACF Import Add-on.

Powered by BetterDocs

Newsletter

Subscribe and stay connected through our Newsletter. We send out important news, tips and special offers.

  • This field is for validation purposes and should be left unchanged.