Support

Account

Home Forums ACF PRO Redirect Form to variable External URL

Solved

Redirect Form to variable External URL

  • Hi,

    I have a front end form which offers the user a choice of a listing plan. Each plan costs a different amount. I would like to redirect the user to the correct payment checkout page using Stripe, upon form submission.

    At the moment the page is just refreshing rather than redirecting. Any help would be most appreciated. Thanks

    // Redirect to Stripe Payment checkout based on user selection.
    add_filter('acf/save_post', 'stripe_payment_redirect');
    function stripe_payment_redirect($post_id) {
    
    	if($post_id != 'new') {
    		return $post_id;
       	}
       
    	if ( have_rows( 'listing_payment_options', $post_id ) ) :
    		while ( have_rows( 'listing_payment_options', $post_id ) ) :
    			the_row();
    			 
    			$package_choice = get_sub_field( 'listing_package_options' ); 
    			$featured       = get_sub_field( 'listing_featured_listing' );
    			
    			if ( $package_choice == '6 Monthly' && $featured == 1 ) :
    				//'6 Monthly and Featured';
    				wp_safe_redirect( 'https://buy.stripe.com/test_3cseYAdSk3D7f6M9AB' ); exit;
    			elseif ( $package_choice == 'Yearly' && $featured == 1 ) :
    				//'12 Monthly and Featured';
    				wp_safe_redirect( 'https://buy.stripe.com/test_dR66s415y1uZ5wceUW' ); exit;
    			elseif ( $package_choice == 'Yearly' ) :
    				//'12 Monthly Only';
    				wp_safe_redirect( 'https://buy.stripe.com/test_dR66s415y1uZ5wceUW' ); exit;
    			else:
    				//'6 Monthly Only';
    				wp_safe_redirect( 'https://buy.stripe.com/test_3cseYAdSk3D7f6M9AB' ); exit;
    			endif;
    	
    		endwhile;
    	endif;
    	
    } ?>
  • I have. It’s in the code above.

  • Missed it, I didn’t scroll the code to the right.

    What type of a field is “listing_featured_listing”?

    Is your code in the theme or in a plugin?

    Are you sure that you’re getting the values that you expect to get?

  • No problem.

    ‘listing_payment_options’ is a group field.
    ‘listing_featured_listing’ is a sub field of that group and is a true/false field.
    ‘listing_package_options’ is also a sub field and is a button group field.

    I pasted the code (from the ‘if have_rows‘ ) into the single post template and it displays the correct field values so I know that is working fine.

    It’s just not redirecting after post submission. Here is my acf form code:

    acf_form(array(		
    				'id'           => 'Submit Listing Form',	
    				'post_id'      => 'new_post',
    				'field_groups' => array('group_610bc08a94306'),	
    				'post_title'   => true,
    				'post_content' => true,		
    				'form'	       => true,
    				//'return'       => $payment_url,
    				'new_post'     => array(
    					'post_type'   => 'post_type_listings',
    					'post_status' => 'pending',
    					),
    				'submit_value'    => __('Submit New Listing', 'hr-listings'),
    			));
  • You are doing this on an acf/save_post hook. That means that this happens after the post is created and this will always be true. So, none of your code is being run.

    
    if($post_id != 'new') {
      return $post_id;
     }
    

    Completely missed that this was with acf_form() and that part of your code.

    It does not even matter that this does not match the post_id setting for acf_form().

    You will need to find another way to test, maybe a combination of is_admin() and get_post_type($post_id)

    
    if (is_admin() || get_post_type($post_id) != 'post_type_listings') {
      return;
    }
    
  • Ahh… I was looking at the wrong bit of code for my fix then!

    It was this snippet that stopped my code running, as you had mentioned:

    if($post_id != 'new') {
      return $post_id;
    }

    I’m not sure though that this check is the right one though:

    if (is_admin() || get_post_type($post_id) != 'post_type_listings') {
      return;
    }

    I have a custom user role on my website that can submit the form. So I’d only want them to be able to submit it and only once. (the form gets saved as pending state before I manually approve it).

    Any tips please?!

    Thank you for you help on this. Most appreciated.

  • I think that would be sufficient. It is an acf_form() on the front end so is_admin() will be true and that is the post type you are creating.

  • Thank you for your help.

    I have one more issue though; it also fires when ‘editing’ a front end post. So I only want this to run when creating a new post. Is there a way to perhaps target that?

    Thanks

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.