0

I'm writing a WordPress plugin. I want to display a custom message after a post is saved. This message will depend on the outcome of function called when the post is saved.

Here's my code:

add_action('save_post', 'my_save_post_function');
function my_save_post_function() {
  global $msg;
  $msg = "Foo bar";
  ...
}

add_filter('post_updated_messages', 'my_post_updated_messages_function');
function my_post_updated_messages_function($messages) {
  global $msg;
  $messages["post"][1] = $msg; // !! $msg is undefined !!
  ...
}

Why is $msg undefined?

Is there any way I can get a result out of a save_post action? I've tried all sorts of tricks. Even the $_POST data seems to have been blown away by the time admin messages are shown.

4
  • 1
    global is generally bad practice, parse the variables though the functions
    – user557846
    May 21, 2013 at 21:54
  • this looks like flash messages, so why not use sessions for this? I know wp doesn't use sessions, but you can start the session in your plugin and go from there. Also, in the way you did your code, is really really bad...it really depends on the order your functions are called and if they are called in the same execution cycle. May 21, 2013 at 21:56
  • Have a read of this, the last section of the article explains how to achieve what you want. May 22, 2013 at 3:23
  • @Dagon, I hate using globals too for this but I can't pass the variables through the functions. The output of my_save_post_function depends on the value of $_POST and $_POST has vanished (!?) when my_post_updated_messages_function get called. I can't determine what the outcome was or should have been by the time my_post_updated_messages_function get called. Hence, the cruddy globals. I'll give sessions a go. Event if that's just another way around globals, it's one I hadn't thought of. Thanks! May 22, 2013 at 10:24

1 Answer 1

1

have you tried session ? i think your problem will be fixed .

take a look at :

http://www.php.net/manual/en/function.session-start.php

1
  • Ugh. Horrible solution (not you're fault!) but it does the job. Thanks! Jun 4, 2013 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.