0

I'm familiar with how to set custom messages for post updates using the post_updated_messages action hook. This question is slightly different.

I have a function/action hook on post_updated:

function my_custom_function($post_id) {
  $response = ping_other_service_and_get_confirmation($post_id);
  syslog(LOG_INFO, 'Response:' . $response['body']);
  return $response['body'];
}
add_action( 'post_updated', 'my_custom_function');

Assuming that the ping_other_service_and_get_confirmation() function pings some other web service, letting it know that the post has been updated, and returns a confirmation message as a string. Right now I'm logging that string to the system logs, which is fine for development, but I need to use that string inside the post updates message for clients to be able to see the confirmation. How can I do that? Is it possible?

  • I guess I could maybe save the response value as a custom field on the post, and then query that in the post_updated_messages filter function, but that seems messy, it's a transitory message, it really shouldn't be stored in the database. – Josiah Sprague May 25 '15 at 13:30
0

I believe you can get the same result what you are looking for with filter hook post_updated_messages. You just need the post id for the confirmation thing that you are doing which you can do over here as well using $_GET['post'] which contains your post id.

Here is function which will do the thing.

function your_modified_message( $messages ){

    if(isset($_GET['post'])){

        $post_id = $_GET['post'];

        // get your response for respective post_id 
        $response = ping_other_service_and_get_confirmation($post_id);
        syslog(LOG_INFO, 'Response:' . $response['body']);

        // get your information concatenated with update post message in next line
        $messages['post'][1] = $messages['post'][1] . '<br>' . $response['body'];

        return $messages;

    } else {

        return $messages;

    }

}
// using filter hook to modify your message
add_filter( 'post_updated_messages', 'your_modified_message',10,1);

Hope this will work for you.

  • Thanks. That would work for the messages, but I need to call the ping_other_service_and_get_confirmation() function on the post_updatedaction (and only on that action). I just need to pass the response over to the post_updated_messages filter. – Josiah Sprague May 25 '15 at 13:26
  • Glad to know that you were able to find some solution on your own. – Mohammad Mursaleen Jun 3 '15 at 5:48
0

I finally figured out a solution using query vars to pass the status message to the next page so that it can be used in post_updated_messages. Here's a simplified version of what I did, with all the function names changed for simplicity's sake to focus on the problem at hand;

First, added the value I needed to the $_POST var;

function my_custom_function($post_id) {
  $response = ping_other_service_and_get_confirmation($post_id);
  $_POST['status_response'] = $response['body'];
}
add_action( 'post_updated', 'my_custom_function');

Then, when that value is set, I redirect the post edit page to the same URL with a query string added that has the value I need;

function pass_status_response($location){
  if (isset($_POST['status_response'])) {
    $location = esc_url_raw(add_query_arg('status_response', $_POST['status_response'], $location));
  }
  return $location;
}
add_filter('redirect_post_location', 'pass_status_response');

Finally, I append that value to the messages in post_updated_messages.

function add_status_message($messages) {
  if ($_GET['status_response']) {
    $post = get_post();
    $post_type = get_post_type($post);
    foreach ($messages[$post_type] as &$msg) {
      $msg = $msg . 'Status: ' . $_GET['status_response'];
    }
  }
  return $messages;
}
add_filter('post_updated_messages', 'add_status_message');

This appears to work fairly well, however, it's very experimental. If anyone has any feedback on how I could improve this solution, I would appreciate it. Thanks!

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.