I'm developing a simple plugin that does additional processing on post text when user saves/updates the post (i.e. clicks Publish post or Update post button).
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');
I have tried hooking onto publish_post
or save_post
hooks, but everytime it processes the post, only the "added text" changes are saved in post. All of the old plugin changes are discarded.
For example if the plugin would change spaces to dashes the problem is as follows:
// 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?
Where is the problem?
my_string_manipulation_function()
so you should update your question to include that in the code.