With WordPress Importer, why can’t I import post meta containing a multi-dimensional array, in which value(s) of that array contain line breaks?

The Question

With WordPress Importer, why can’t I import post meta containing a multi-dimensional array, in which value(s) of that array contain line breaks?

Is there a workaround that I can do to save my data so it will work? Is there something I can do in my sanitation proces to format the data differently before saving it to the post meta?

Read More

The Background Info

I’ve created a WordPress plugin that provides a layout builder. Users can construct the content for a custom layout with certain “elements” given to them (i.e. a slider, a set of columns, a content input, etc), and then apply that custom layout to a static page on their site.

Originally, my goal was for all custom layouts to work with WordPress’s import/export tool. So each custom layout is actually a custom post type post, and its respective set of “elements” are saved as post meta to that post.

So, essentially I’m saving this single multi-dimensional array with all “elements” as a custom field to the custom post, which is the custom layout.

The Problem

If any of a custom layout’s “elements” contain any user-inputted text with a line break, it prevents that post meta from being imported through Tools > Import > WordPress. The result is that the the custom post (i.e. custom layout) gets imported but the element’s post meta is empty. So the user sees a blank layout with no elements.

So, this first example has no line breaks and does work.

However, in this second example the user has put in a line break and so it does not work and the problem described above occurs.

Possible Solutions?

I’d really like to be able setup sample content for users and give them the WordPress import file so they can quickly import these sample layouts.

So, I’m just trying to figure out if anything can be done to avoid this? Is it possible within my sanitization process I can format the data any differently before I store it in the post meta? Obviously I’d just strip out all the white space, but then the user won’t be able to format their text within the options of the layout builder.

Related posts

Leave a Reply

1 comment

  1. I would wage that your issue is with rn (CRLF). If the newlines are created in Windows, they’ll go into the database as rn and when serialized get counted as 2 characters. Then when importing, they’ll only be counted as 1 character and they’ll corrupt the string. I tested this hypothesis and sure enough I was able to create a corrupt serialized string.

    If this is your issue, the solution would be to replace "rn" with "n" when saving the post meta. This is a long shot, but I can’t think of what else might cause this behavior. Give ‘er a shot and let me know how it works out!