发布/编辑后的POST文本清理-未保存更改

时间:2014-09-11 作者:Joudicek Jouda

我正在开发一个简单的插件,当用户保存/更新帖子(即单击发布帖子或更新帖子按钮)时,它会对帖子文本进行额外处理。

function call_my_function_after_edit_post($post_id)
{
    // If this is just a revision, don\'t do anything
    if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ))
        return $post_id;

    $text = my_string_manipulation_function($_POST[\'post_content\']);
    $excerpt = my_string_manipulation_function($_POST[\'post_excerpt\']);

    /* TESTING */
    $content = "Old content: \\r\\n".$_POST[\'post_content\']."\\r\\n\\r\\nNew content: \\r\\n".$text;
    $fp = fopen($_SERVER[\'DOCUMENT_ROOT\'] . "/test.txt","wb");
    fwrite($fp,$content);
    fclose($fp);
    /* END OF TESTING */

    // unhook this function so it doesn\'t loop infinitely
    remove_action(\'publish_post\', \'call_my_function_after_edit_post\');
    wp_update_post(array(\'ID\' => $post_id, \'post_content\' => $text, \'post_excerpt\' => $excerpt));
    add_action(\'publish_post\', \'call_my_function_after_edit_post\');
}
add_action(\'publish_post\', \'call_my_function_after_edit_post\');
我已经试过了publish_postsave_post 挂钩,但每次处理帖子时,只有“添加的文本”更改保存在帖子中。所有旧插件更改都将被丢弃。

例如,如果插件将空格更改为破折号,则问题如下:

// User starts creating post with text:
Hello world
// User saves the post and the text is changed via the plugin to:
Hello-world
// User decides to add additional text:
Hello world, how are you?
// Plugin should take all the text and process it so expected result is:
Hello-world,-how-are-you?
// But it ends like this:
Hello world,-how-are-you?
问题出在哪里?

1 个回复
最合适的回答,由SO网友:Joudicek Jouda 整理而成

正如@karpstrucking在评论中提到的,问题最终在于我的功能:my_string_manipulation_function()

我在使用preg_replace() 来处理母语字符的字符串,但我没有使用/u 用于指定我正在使用的标记修饰符unicode characters.添加此标志修复了所有奇怪的行为。

结束

相关推荐