WordPress.org

Support

Support » Plugins and Hacks » Hacks » wp_insert_post_data and sessions – strange behaviour

wp_insert_post_data and sessions – strange behaviour

  • Hello, I’m trying to process the post content before saving it to database and inform user if there was any error. I’m using wp_insert_post_data filter, sessions and admin_notices, since it seems to be the only simple solution to throw the error message to edit screen in admin panel after publishing. However, I ran into strange problem – the code seems to run twice, so when I have:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I receive two messages. I’ve made some tests and it seems that first is for content that is to be saved in database and second for the “old” content (already saved). For example: if previous content was “wrong” and the one to be saved is “ok”, the first message is “ok” and second “error” and so on. However, all messages are generated on the same time (so this is not some kind of caching problem, I suppose).
    What is even more strange for me, is that if I use this simple code:

    add_filter ('wp_insert_post_data', 'filterthis', '99', 2);
    function filterthis($data){
    $date = date('H:i:s');
    $_SESSION['my_admin_notices'] .= '<div class="updated"><p>This is a message from '.$date.'</p></div>';
    $data['post_content'] .= $date;
    return $data;
    }

    I get also two messages, but only one piece of timing data appended to post content – identical to the one delivered by the first message. So it seems like the code runs twice, but only for the first time it saves the content to database… I’m totally confused after seven hours of googling and reading Codex. Maybe the solution is trivial, but I’m not really a PHP programmer, just learning it for a while, so I would be very glad if someone here could help me.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘wp_insert_post_data and sessions – strange behaviour’ is closed to new replies.
Skip to toolbar