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");
    /* 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:
// User decides to add additional text:
Hello world, how are you?
// Plugin should take all the text and process it so expected result is:
// But it ends like this:
Hello world,-how-are-you?

Where is the problem?

  • 1
    The problem could be in my_string_manipulation_function() so you should update your question to include that in the code. – karpstrucking Sep 11 '14 at 11:32
  • Double that. No chance to know what's going on without that. Is that code from a 3rd party plugin or theme? – kaiser Sep 11 '14 at 11:43

As @karpstrucking mentioned in the comments, the problem was in the end with my function: my_string_manipulation_function()

I was using preg_replace() to process the string of native language character, but I didn't used the /u flag modifier to specify that I'm working with unicode characters. Adding this flag fixed all weird behaviour.

| 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.