Home > OS >  Wordpress CF7 cannot redirect on mail sent action
Wordpress CF7 cannot redirect on mail sent action

Time:10-01

I am trying to redirect my page to a url i get from a curl post response.
This is my code:

add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent'); 
function action_wpcf7_mail_sent( $contact_form ) {
    if ( $contact_form->id !== 127 )
        return;
    $wpcf7 = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();
    $save = $submission->get_posted_data();
    $data = //the data I send
    $url= 'http://my-url';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']);
    $response = json_decode(curl_exec($ch));
    curl_close ($ch);
    //I need to redirect my contact form to response['redirect_url']
}; 

I need to redirect my contact form to response['redirect_url']
I tried multiple things but they all did not work:
wp_redirect($response["redirect_url"]);
header( 'Location: '.$response["redirect_url"] );
Both using exit; and

?><script type="text/javascript">
        location = <?php echo $response["redirect_url"] ?>;
</script><?php

But they all gave an error. Please let me know if you have any ideas!

EDIT
I tried changing my code a little!
I tried to catch the cf7mailsent from js and then call the php function like so:

document.addEventListener( 'wpcf7mailsent', function( event ) {
  var inputs = event.detail.inputs;
  $.ajax({
    type: "POST",
    datatype:"json",
    url:jsforwp_globals_sub.ajax_url,
    data: { 
        action:"send_this_data",
        ajax_nonce:jsforwp_globals.send_info,
        info:inputs,
    }
  }).done(function(mess) {
    msg=jQuery.parseJSON(mess);
    if(typeof msg['redirect_url']!="undefined"){
        window.location.href = msg['redirect_url'];
    }else if(typeof msg['message']!="undefined"){
        alert(msg['message']);
    }else{
        alert('failed' mess);
    }
  }).fail(function(xhr, status, error) {
  });
});
add_action("wp_ajax_nopriv_send_this_account","action_wpcf7_mail_sent");
function action_wpcf7_mail_sent() {
    $save=$_POST["inputs"];
    //the rest of the code from $save = $submission->get_posted_data();
    echo json_encode($response);
    wp_die();
}

but it is not working, what would be the right solution for this?
Big thanks!

more info
The issue is when I try using the $save, I am pretty sure it gives null

CodePudding user response:

CF7 plugin does not provide such sophistication, and the only way to solve this problem is to trigger a double redirect, the first one using the CF7 JavaScript event triggered redirection as documented in the plugin page, and the second one on the server using wp_redirect() function.

Step 1, use the CF7 redirection, which requires you to hardcode the link at the time of loading the form on your page. Create a default page for this, say form-redirection,

add_filter('wpcf7_form_hidden_fields','add_hidden_nonce');
function add_hidden_nonce($fields){
  $nonce = wp_create_nonce('cf7-redirect-id');
  $fields['redirect_nonce'] = $nonce;
  /*
   hook the shortcode tag filter using an anonymous function 
   in order to use the same nonce and place a redirect js event 
   script at the end of the form. 
  */
  add_filter('do_shortcode_tag', function($output, $tag, $attrs) use ($nonce){
    //check this is your form, assuming form id = 1, replace it with your id.
    if($tag != "contact-form-7" || $attrs['id']!= 127) return $output;
    $script = '<script>'.PHP_EOL;
    $script .= 'document.addEventListener( "wpcf7mailsent", function( event ){'.PHP_EOL;
    //add your redirect page url with the nonce as an attribute.
    $script .= '    location = "'.esc_url(home_url('/form-redirection/?cf7='.$nonce)).'";'.PHP_EOL;
    $script .= '  }'.PHP_EOL;
    $script .= '</script>'.PHP_EOL;
    return $output.PHP_EOL.$script;
  },10,3);
  return $fields;
}

NOTE: the introduction of a unique nonce ensures you can uniquely identify the submission. The nonce is added as a hidden field in your form (which you can retrieve at submission) as well as on the redirection url, to identify which submission is being redirected.

Step 2: do your submission processing using your function fired on the 'wpcf7_mail_sent' hook in your answer, and store the resulting $response results in a transient using the unique nonce introduced in step 1,

add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent'); 
function action_wpcf7_mail_sent( $contact_form ) {
    if ( $contact_form->id !== 127 ) return;
    //make sure the nonce is available too,
    if(!isset($_POST['redirect_nonce']) return;
    ...
    $response = json_decode(curl_exec($ch));
    curl_close ($ch);
    //I need to redirect my contact form to response['redirect_url']
    //so store in a transient
    set_transient('_cf7_data_'.$_POST['redirect_nonce'], $response['redirect_url'], 5*60); //5 min expiration.
}

Step 3: on your redirect page, detect the form submission, fetch the stored response data from the transient, and redirect to your final location...

add_action('init', 'redirect_cf7_submission');
function redirect_cf7_submission(){
  if( isset($_GET['cf7']) ){
    $transient = '_cf7_data_'.$_GET['cf7'];
    $url = get_transient($transient);
    if(false === $url) $url = home_url(); //or back to the form page?
  }else $url = home_url(); //or back to the form page?
  wp_redirect($url);
}
  • Related