Support

Account

Home Forums Front-end Issues Front-end Form: Redirect after Save

Solved

Front-end Form: Redirect after Save

  • Hey guys,

    first: i love this plugin, really.

    i created a frontend from with the following function:

    $args = array(
    'post_id' => 'new',
    'field_groups' => array( 14 ),
    'submit_value' => 'Submit',
    );
    acf_form( $args ); 

    and it’s work fine. Now, i will redirect the current user after submit the form. in your documentation i found the hook acf/pre_save_post and created this function for me:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    			
    	$random = rand(0,999);
    			
        if($post_id != 'new') {
    		return $post_id;
       }
    		 
    	// Create a new post
    	$post = array(
    		'post_status'  => 'publish' ,
    		'post_title'  => $current_user->ID.' - '.$_POST["fields"]['field_5326a5dde1c6f'],
    		'post_type'  => 'eplan',
    	);  
    		 
    	// insert the post
    	$post_id = wp_insert_post($post); 
    		 
    	// update $_POST['return']
    	$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );    
    		 
    	// return the new ID
    	return $post_id;
    	
    	do_action( 'acf/save_post' , $post_id );
    	wp_redirect( add_query_arg( 'updated', 'true', get_permalink( $post_id ) ));
    	exit();
    }

    the post will be created but… no redirect, why? nothing happens.

    Greetings
    Christian

  • push…

    No one has an idea?

  • Bump. Also really need this.

  • Hi StormTrooper,

    this code works for me:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
        global $post;
    	
    	if($post->post_name == 'some-post') {
    	    // Create a new post
    	    $post = array(
    	        'post_status'  => 'publish' ,
    	        'post_title'  => 'some-title',
    	        'post_type'  => 'some-post-type',
    	    );  
    	 
    	    // insert the post
    	    $post_id = wp_insert_post($post); 
    		
    		// update custom field
    		update_post_meta($post_id, 'some-customfield', $post_id);
    						 
    		do_action('acf/save_post', $post_id);
    
         	//do_action('acf/save_post' , $post_id);
    	    wp_redirect( '/some-page-to-redirect/', 301);
    		exit;	
    	}			
    }
    

    Check your site for Javascript errors if the function does not work… 🙂

    Greetings
    Christian

  • Hi Christian, thanks for getting back on this 🙂

    I also just managed to get it to work by removing the $_POST['return'] line. So I ended up with this:

    
    $post_id = wp_insert_post($post); 
    
        do_action( 'acf/save_post' , $post_id ); // Save the fields to the post
        wp_redirect( add_query_arg( 'updated', 'true', get_permalink( $post_id ) ) ); exit; // Redirect to the new post
        
        return $post_id;
    

    you can also replace get_permalink( $post_id ) with a specific URL.

  • Similar to StormTrooper’s solution – simply adding this to my theme’s functions.php file did the trick. (Not sure if it is the best / right way.)

    function my_acf_save_post( $post_id )
    {
        wp_redirect(get_permalink($post_id)); exit;
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • Thanks @slickorange for code, simple and do the job!

  •    $post = array(
    	        'post_status'  => 'publish' ,
    	        'post_title'  => 'some-title',
    	        'post_type'  => 'some-post-type',
                     'return' => 'custom url (www.whatever.com) or home_url'
    	    ); 

    From the ACF documentation

    return The URL to be redirected to after the post is created / updated. Defaults to the current URL with an extra GET parameter called updated=true. A special placeholder of %post_url% will be converted to post’s permalink (handy if creating a new post)!

  • Thanks guys, it works great.

    Question: how would you about create multiple forms on the same page?

    I need to build a page with multiple tabs, each one has a button to create a new post in a different post type.
    Example:
    tab 1: <button>Create new store</button>
    tab 2: <button>Create new employee</button>
    tab 3: <button>Create new team</button>

    Since the post type is defined in the arguments in PHP I cannot figure out how to make this dynamic, example: you click on tab 1 the post type for the presaved post will be “Store”, you click on tab 2 then the button saves an “employee”… and so on.

    Any help will be welcome,

  • @endcore or someone i need some help immediately.I can’t redirect after user submit the form

    <?php
    acf_form_head();
    ?>
    <?php
    $args = array(
    ‘post_id’ => ‘new_post’,
    ‘new_post’ => array(
    ‘post_type’ => ‘corona_data’,
    ‘post_status’ => ‘publish’
    ),
    ‘submit_value’ => __(‘Αποστολή’)
    );
    acf_form($args);
    ?>

    and my function in functions.php

    add_filter(‘acf/pre_save_post’ , ‘my_pre_save_post’ );
    function my_pre_save_post( $post_id ) {
    global $post;

    if($post->post_name == ‘some-post’) {
    $post = array(
    ‘post_status’ => ‘publish’ ,
    ‘post_title’ => ‘some-title’,
    ‘post_type’ => ‘some-post-type’,
    );
    // Create a new post

    // insert the post
    $post_id = wp_insert_post($post);

    // update custom field

    do_action(‘acf/save_post’, $post_id);

    //do_action(‘acf/save_post’ , $post_id);

    wp_redirect(‘https://coronavirus.crowdapps.net/stoixeia-forea/&#8217;, 301);
    exit;
    }
    }

  • Hello, please how to display active events first then the expired events , i need code!

    am using ACF 5.7.10

    Screenshot :
    https://imagehost.imageupload.net/2020/04/23/FireShot-Capture-024—Evenements—mesloisirs—www.creativejobgroup.com.jpg

    please please help me , am beginner on ACF

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

The topic ‘Front-end Form: Redirect after Save’ is closed to new replies.