Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I would wage that your issue is with \r\n (CRLF). If the newlines are created in Windows, they'll go into the database as \r\n 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 "\r\n" 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!

share|improve this answer
    
So awesome! Thank you so much. I can't believe that worked: picturestack.com/286/25/01CScreenShotZwn.png So, you said that happens on Windows, but I'm not doing anything on Windows. I'm just curious why this is working? Very interesting. –  Jason Feb 15 '12 at 6:08
    
Are the posts filled out by users on Windows? That's what would do it, because the \r\n is added when the user creates a newline -- not when the server stores it. Also, just to be safe, instead of replacing "\r" with "", you should replace "\r\n" with "\n", that way if newlines are entered as just "\r" they won't be wiped. I'll update my post to reflect that. –  Matthew Boynes Feb 15 '12 at 14:05
    
Thanks for the tip, I'll definitely change it to that. Aw ok... So, you're referring to the user in The WordPress admin panel typing into the text area and saying they're on a Windows computer. This has been a problem with this plugin for awhile, so I'm sure that is the case quite often. However, interestingly, I do not use Windows at all. I'm a Mac user. So all of my tests and development happen on a Mac, including all screenshots originally attached to this question and all of my experiments in the past. –  Jason Feb 15 '12 at 18:43
    
So, it appears this happens on a Mac, too. I've been playing around with this more since your answer, and I've also noticed that if I manually feed an array through the PHP with an actual line break (opposed to just typing "\n"), it's also using "\r\n" and doing the same thing. –  Jason Feb 15 '12 at 18:43
    
That sure is funny! I wonder if WordPress 'normalizes' the newlines or something. Great tip though, thanks for the followup! –  Matthew Boynes Feb 15 '12 at 19:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.