WordPress 1.2 有个“特性”,写在 Blog 内容里的所有的单个反斜杠 "" 都会在显示时或者再次编辑时消失,而成对的反斜杠则会在显示或者再次编辑时变成一个。如果仅仅是显示的问题,也就罢了,要用反斜杠时写成两个就行了。但每次在 WordPress 里编辑以前的 Blog 时反斜杠也会被“吃掉”,这是件很让人头疼的事,好好的文章编辑几次所有的反斜杠都没有了。

在 WordPress 的 Support 上搜索 "backslash",找到一些帖子。有人说这个问题在 1.3 里已经修正了,也有人提供了 1.2 下解决的办法,具体做法如下。 打开 WordPress 安装目录下的 wp-includes\functions-formatting.php 文件,搜索 "format_to_edit" 会在 266 行附近找到这个函数,它看起来是这样的

function format_to_edit($content) {
    $content = stripslashes($content);
    $content = apply_filters('format_to_edit', $content);
    $content = htmlspecialchars($content);
    return $content;
}

改成这样就可以避免在编辑 Blog 时反斜杠被“吃掉”。

function format_to_edit($content) {
    // 注释掉这行代码可以避免编辑时的反斜杠丢失问题;
    //$content = stripslashes($content);
    $content = apply_filters('format_to_edit', $content);
    $content = htmlspecialchars($content);
    return $content;
}

继续打开 WordPress 安装目录下的 wp-includes\functions.php 文件,搜索 "start_wp" 将会在 1056 行附近找到这个函数,在它的结尾处有这样一些代码

    if (preg_match('/<!--nextpage-->/', $post->post_content)) {
        if ($page > 1)
            $more = 1;
        $multipage = 1;
        $content = stripslashes($post->post_content);
        $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
        $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
        $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
    } else {
        $pages[0] = stripslashes($post->post_content);
        $multipage = 0;
    }

把它们改成这样就可以避免在 Blog 显示时反斜杠被“吃掉”。

    if (preg_match('/<!--nextpage-->/', $post->post_content)) {
        if ($page > 1)
            $more = 1;
        $multipage = 1;
        // 修改这两行代码可以避免显示时的反斜杠丢失问题;
        //$content = stripslashes($post->post_content);
        $content = $post->post_content;
        $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
        $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
        $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
        $pages = explode('<!--nextpage-->', $content);
        $numpages = count($pages);
    } else {
        //$pages[0] = stripslashes($post->post_content);
        $pages[0] = $post->post_content;
        $multipage = 0;
    }

OK, Enemy down! ;)

WordPress Support 上的原文

Tags: ,

2 Responses to “让 WordPress 不再“吃掉”反斜杠 "\"”

  1. ken Says:

    xx,你个xx
    偶来鸟

  2. Rainux Says:

    Enemy spotted!
    ......
    Enemy down!

Leave a Reply