How to change a form's success message when a post is updated

Created by Phil Kurth, Modified on Thu, 28 Jul 2022 at 02:30 PM by Phil Kurth

When using a form to manage posts, you may wish to change the success message if a post is updated. You may do so using a combination of hooks and passing data between them via the Advanced Forms `submission` array. 


<?php

// First, hook into the post updated hook to capture desired data for a future hook to use.
// The data is stored on the AF()->submission array which is accessible anywhere.  
add_action( 'af/form/editing/post_updated', function ( $post, $form, $args ) {
  // Only run on a specific form.
  if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
    return;
  }
  
  // Be sure to replace `xyz` with your own custom prefix to avoid potential future issues.
  AF()->submission['xyz_post_id_updated'] = $post->ID;
}, 10, 3 );

// Second, hook into the success message filter and check for our custom data key. If available,
// modify the success message.
add_filter( 'af/form/success_message', function ( $success_message, $form, $args ) {
  // If we don't have our custom key available, return default success message.
  if ( empty( AF()->submission['xyz_post_id_updated'] ) ) {
    return $success_message;
  }

  // Get the stored post object
  $post_id = AF()->submission['xyz_post_id_updated'];
  return "Post $post_id updated.";
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article