How to use filter hook ‘post_updated_messages’ in coherence with action hook ‘save_post’

Updated:

First, you will need to return a bool value on your notifications method so we can reliably set a marker for the message method. Then, you will need to set a $_POST array element to pass on to the redirection filter.

public function save_post($post_id){
    //Add a $_POST key if you syndicated successfully
    if($this->send_group_notifications()) //return true from your notification method
        $_POST['syndicated'] = true;
}

The following filter works from your functions.php file. I couldn’t get it to work properly from a plugin file. Basically, what’s happening here is we sniff out the $_POST array element you set in the save_post action and see if we add a query string variable to the redirect.

add_filter('redirect_post_location', 'update_post_redirect');
function update_post_redirect($location){
    $syndicated = isset($_POST['syndicated']) ? $_POST['syndicated'] : 0;
    if($syndicated == true)
        $location .= '&syndicated=1';
    return $location;
}

Then finally, we sniff out the $_GET variable so we can determine if a message needs to be displayed.

public function message($messages){
    $syndicated = isset($_GET['syndicated']);
    if($syndicated)
         //Do something with the messages array here.
    return $messages;
}

Check out this post for a nice example of how to use this filter: Set custom messages for post update/save

Hope this helps you out!

Leave a Comment