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

  • Actually, sorry but this isn’t working.

    It just redirects to the post submission page. When I remove the following:

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

    and edit a post, it then redirects to the correct payment page. But still does not redirect to a payment page upon initial form submission.

    Strange?! Any ideas please!?

    Many thanks

  • Is your filter in the theme or in a plugin?

  • Make sure that the post type is correct, I was going by your wp_form() code

    
    
    				'new_post'     => array(
    					'post_type'   => 'post_type_listings',
    					'post_status' => 'pending',
    					),
    

    I don’t see any reason why the redirect is not working

    Honestly, the if statement shouldn’t even be needed because none of the redirects should do anything unless this is true

    
    if ( have_rows( 'listing_payment_options', $post_id ) ) :
    

    and this should only be true on the correct post type the only thing you should really need to check is is_admin()

    This should not help because you have the action in the theme, but you could try increasing the priority to > 10

    
    add_filter('acf/save_post', 'stripe_payment_redirect', 20);
    
  • Thanks for the help. But it’s still not redirecting with:
    add_filter('acf/save_post', 'stripe_payment_redirect', 20);

    I’ve tried removing the if statement and it still doesn’t redirect to payment.
    I need some sort of if statement though because I have the ability for users to edit the submitted post. And I do not want the redirect to fire when they are only editing.

    The strange thing is; when the if statement is removed; that when editing the submitted post, the redirect works?!

  • I have since found out that the $post_id is actually the page id where the acf form is located and not the actual post id for the post submitted…

    How do I grab the submitted post id or data?

    But still, I do not how to resolve this. Any help would be much appreciated.

  • with 'post_id' => 'new_post', in acf_form() the post should not be the post ID of the page.

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

You must be logged in to reply to this topic.