4

When I want to import the XML files with the aid of the WordPress Importer plugin, the blogs imported will lose all the slashes, i.e., "\", which are really contained in the XML files imported.

Since I have to use the LaTeX plug-in (Latex for WordPress plug-in), it becomes terrible.

In fact, maybe weeks ago, it worked well. There was no such problem at all. But from one day, I find that all changed. TT

Is there any one who know what's happening? Thank you very much!

  • Are the slashes removed during import or are they imported and just not displayed? Does it happen if you use TwentyEleven and disable all other plugins? – fuxia Apr 28 '12 at 14:53
  • Yes, they all disappeared during import. I tried, i.e., disabled all plug-ins, but the problem is still there. – shuiruge Apr 29 '12 at 5:03
4

The importer calls wp_insert_post() which runs stripslashes_deep() on the data. But the exporter doesn't run addslashes. Looks like a bug to me.

What you may try is to prepare the data during export.

Create a plugin with the following content and activate it before you run the export:

<?php
/**
 * Plugin Name: Slash my export
 * Description: Adds extra back slashes to exported data.
 */

add_filter( 'the_content_export', 'addslashes' );
add_filter( 'the_excerpt_export', 'addslashes' );

I have not tested this, it may be wrong, it may have side effects. stripslashes_deep() may eat the extra slashes too …

You should open a bug on Trac if this plugin fixes your problem.

| improve this answer | |
  • So professional! It can add slashed when export, I guess. While, what I want to do is to add slashes when import. Is there any problem if I add slashes in this way when import, rather than when export? (I guess there is. Because if there is not, there will be no need to run stripslashes_deep() when import. I'm a new guy.) Thanks very much! – shuiruge Apr 29 '12 at 11:03
  • I'm not sure. You would have to apply addslashes before the slashes are stripped … which happens very early. Because once they are removed there is no way to say where they should have been preserved. Changing the export is much easier. – fuxia Apr 29 '12 at 11:13
  • Over three years later, with WordPress 4.2.2, this is still an issue. This plugin seemed to work for posts. I don't think it handled comments--I found one comment with backslashes stripped out. – Mark Berry Jun 4 '15 at 0:03
  • @MarkBerry WP doesn't have a filter for comment content in the exporter, as it does for post content. This is a nasty oversight, as is the lack of addslashes() to begin with. – Michael Hampton Feb 2 '17 at 5:13
  • The fix that was implemented (see my answer) was made to the importer plugin and wound adding slashes to each type of content before calling the import proc (which I guess is in WordPress core?). Although not included in the initial fix, I found a way to apply the same approach to comments as well. – Mark Berry Mar 31 '17 at 1:18
1

The missing backslash issue was reported to WordPress here:

https://core.trac.wordpress.org/ticket/21007

and mostly fixed in 0.6.3:

https://wordpress.org/plugins/wordpress-importer/#changelog

Unfortunately, the fact that backslashes are missing in comments as well was not reported and has not been fixed. I've submitted a pull request that I hope will fix it:

https://github.com/boonebgorges/wordpress-importer/pull/6

Note that the plugin also has a new bug that assigns an Uncategorized category to every post even if it does have a category. That's not related to this question, but it's something to note if you are considering using this plugin. Discussion here:

https://core.trac.wordpress.org/ticket/39301

Update

The fix for backslashes in comments should be in the next minor release, 0.6.4:

https://core.trac.wordpress.org/ticket/21007#comment:11

| improve this answer | |
0

I recently ran into this problem as well, and came up with an a solution that worked for me. In your wordpress install folder, browse to the subfolder wp_includes and open post.php. My version contained the line:

$data = wp_unslash( $data );

(The wordpress function reference manual says that wp_unslash is the replacement to the deprecated stripslashes_deep(), so in an older version of wordpress it may be using stripslashes_deep instead of the command above.)

I commented this line out, re-imported, and all my LaTeX backslashes were there. Hope this helps.

| improve this answer | |
0

I found it ended up being easiest to just do a global search and replace for single backslashes to double backslashes in a text editor. I was worried it might escape some important stuff in the XML, so the following bit of PowerShell will tell you the XML tag that surrounded your backslashes.

$r = Select-String '<([^>]*)>.*?\\.*?\1' .\wordpress.2015-09-13.xml -AllMatches
$matches = $r | % { [PSCustomObject] @{ Tag = $_.Matches.Groups[1].Value; LineNumber = $_.LineNumber; Line = $_.Line } }
$matches

In my case, all (except one) were HTML tags from the post content itself.

PS> $matches | Foreach-Object Tag | Sort -Unique
/font
/SPAN
BR
content:encoded
div
font color="#000000"
FONT face="Courier New"
FONT face=Times color=#000000
LI
P
span style="color: "
span style="color: #000000"
SPAN style="COLOR: #006400"
span style="color: #cccc66; background-color: #012456"
SPAN style="COLOR: blue"
SPAN style="COLOR: green"
SPAN style="COLOR: maroon"
wp:comment_author

I took a look at the wp:comment_author post (somebody had backslashes in their name), and their name imported fine into that post.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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