Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save barrykooij/0e29417bd3e5a6aeee21660686c4ef19 to your computer and use it in GitHub Desktop.
Save barrykooij/0e29417bd3e5a6aeee21660686c4ef19 to your computer and use it in GitHub Desktop.
// Gravity Forms Dynamic Redirect
add_filter( 'gform_confirmation', 'dlm_gf_dynamic_redirect', 10, 4 );
function dlm_gf_dynamic_redirect( $confirmation, $form, $entry, $ajax ) {
$my_label = 'What Download?'; // set this to the exact label of your download select field
// fetch download ID from select
$selected_download = 0;
if ( isset( $form['fields'] ) && is_array( $form['fields'] ) && count( $form ) > 0 ) {
foreach ( $form['fields'] as $field ) {
if ( 'select' === $field['type'] && $my_label == $field['label'] ) {
$selected_download = absint( $entry[ $field['id'] ] );
break;
}
}
}
// check if we found a download, if we did replace our hidden's lead field value
if ( $selected_download > 0 ) {
$dlm_gf_handler = new DLM_GF_Gravity_Forms_Handler();
$lead_id = $dlm_gf_handler->get_lead_index_of_dlm_field( $form );
$entry[ $lead_id ] = $selected_download;
// check
if ( null !== $lead_id ) {
if ( isset( $entry[ $lead_id ] ) ) {
$download_id = absint( $entry[ $lead_id ] );
// create download
$download = new DLM_Download( $download_id );
// check URL
if ( '' != $download->get_the_download_link() ) {
// set redirect
$confirmation = array( 'redirect' => $download->get_the_download_link() );
// set cookie ourselves
$dlm_gf_handler->set_entry_id_cookie( $entry, $form );
}
}
}
}
return $confirmation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment