Redirect to an edit page after post creation

Created by Phil Kurth, Modified on Mon, 11 Aug at 12:57 PM by Phil Kurth

If you have a situation where you have a post creation page and a post editing page, you can use code to redirect from the creation page to the editing page after the post has been created.


<?php
add_action( 'af/form/submission', function ( $form, $fields, $args ) {

  // Check if this is the correct form. If not, stop here.
  if ( $form['key'] !== '{YOUR_FORM_KEY}' ) {
    return;
  }

  // Get the ID of the post that was just created
  $post_id = AF()->submission['post'];

  if ( $post_id ) {
    // Build the edit page URL (replace '/your-edit-page/' with your actual edit page path)
    $redirect_url = add_query_arg( 'post', $post_id, home_url( '/your-edit-page/' ) );

    // Redirect the user
    wp_redirect( $redirect_url );
    exit;
  }

}, 20, 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 at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article