Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save faisalahammad/eb687aaf2b582ab36cb5a97442d69c70 to your computer and use it in GitHub Desktop.
Save faisalahammad/eb687aaf2b582ab36cb5a97442d69c70 to your computer and use it in GitHub Desktop.
This snippet demonstrates how to automatically redirect users to the newly created post after submitting a Gravity Forms form using the Advanced Post Creation add-on in WordPress. Perfect for guest post submissions, this code ensures users are sent directly to their published post upon form submission.
<?php
/**
* Gravity Forms: Redirect to Newly Created Post After Submission
*
* @author Faisal Ahammad <[email protected]>
*
* Replace 123 with your actual Gravity Form ID.
*/
add_filter( 'gform_confirmation_123', 'gfapc_redirect_to_created_post', 10, 4 );
function gfapc_redirect_to_created_post( $confirmation, $form, $entry, $is_ajax ) {
// Get the post(s) created by the Advanced Post Creation add-on
$created_posts = gform_get_meta( $entry['id'], 'gravityformsadvancedpostcreation_post_id' );
if ( ! empty( $created_posts ) && is_array( $created_posts ) ) {
$post_id = $created_posts[0]['post_id'] ?? $created_posts[0];
$redirect_url = get_permalink( $post_id );
if ( $redirect_url ) {
$confirmation = array( 'redirect' => $redirect_url );
}
}
return $confirmation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment