我正在开发一个简单的插件,当用户保存/更新帖子(即单击发布帖子或更新帖子按钮)时,它会对帖子文本进行额外处理。
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_post
或
save_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?
问题出在哪里?