Support

Account

Home Forums ACF PRO FR: acf_form return to permalink

Solved

FR: acf_form return to permalink

  • Hello again!

    Another small feature request came up that I thought I would pass along concerning the acf_form() parameters.

    The return=> parameter can currently redirect anywhere I want it to, except if I want it to go to the permalink for the post I just created. This can be an issue in a situation where I want a client to view the live post immediately after they create it, or if the edit post form is being displayed on the single_post template.

    My suggestion is that the ‘return’ parameter can take a new value of ‘permalink’ which would redirect the user to the newly created post after the initial creation.

    I’m using this little function to do it now (in case anyone reading this needs a workaround), but it would be great to have it integrated. Obviously, this is a very minor issue, please feel free to focus on bigger, important things first!

    //Custom redirected after creating a post
    function custom_redirect_function_name($post_id) {
    	if (!is_admin) { // make sure we're on the front-end
    		// redirect (we redirect from here instead of on the page template because we don't have the new post ID until now)
    		$redirect = get_the_permalink($post_id);
    		wp_redirect($redirect);
    		exit;
    	}
    }
    add_action('acf/save_post', 'custom_redirect_function_name', 20);
  • Hi @arcanepsyche

    Thanks for the feature request, but this already exists!

    Just place the string %post_url% within the return setting, and the new post’s permalink will be swapped in!

    Fore example:
    'return' => '%post_url%?foo=bar'

  • Just updated the docs too

  • That’s awesome, thank you!

  • This replacement is great, but I need one for post ID also, not URL.
    This is because my custom post type don’t have own URL, only URL parameter (like /dashboard/<CPT-ID> )

    But its also much more versatile because people can do anything they want with the ID.

    So please can you add also:

    $form['return'] = str_replace('%post_id%', $form['post_id'], $form['return']);

  • For some reason I can’t get this to work, The page is getting created, but I get redirected to a blank page, my current Front End page looks like:

    
    <?php                    
      $args = array(
        'post_id' => 'new_1',
        'field_groups' => array( 357 ),
        'submit_value' => 'Submit Story', 
        'return' => '%post_url%?foo=bar'
       );
    acf_form( $args );
    ?>
    

    And My Functions File:

    
    //ACF FORM ARGUMENT
    function my_pre_save_post( $post_id )
    {
        // check if this is to be a new post
        if( $post_id != 'new_1' )
        {		
    		return $post_id;
    	}
    	
    	// Create a new post
    	$post = array(
    		'post_status'  => 'publish' ,
    		'post_title'  => $_POST['fields']['field_54f5894e01b06'],
    		'post_content'	=>	$_POST['fields']['field_54f5897401b07'],
    		'post_type'  => 'shortstory',
    		'post_parent'   =>  $_POST['fields']['field_54f596ac44e2b']
    	);  
    
    	// 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 $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    

    Any help would be much appreciated.

  • I would like to see that post_id gets added to the return url.

    If I for example want to redirect them to a thank you page where I want to do additional with the added post I need the post_id

    So i would like to have a a url like http://samplesite.com/thankyou/?id=###

  • The URL does not change, the page refreshes (stays on the same URL) but the page becomes blank, without having any errors on it.

  • Hi @meserlian.

    I just happened to me. I put WP_DEBUG a true and I appeared the following error:

    Warning: Cannot modify header information - headers already sent by...

    It was solved by testing the following.

    – Put on the first line of your document. Above the DOCTYPE:

    <?php acf_form_head(); ?>

    Do not understand why it works but it does.

    I’m reviewing the plugin files to see how to fix it.

    In the funciones.php file I have nothing. That code is never executed.

    Only I have in my template.

    	$date = date('d/m/Y');
    
    	$options = array(
    		'post_id' => 'new_post',
    		'new_post' => array(
    		'post_type' => 'appointment',
    		'post_status' => 'draft',
    		'post_title' => 'Cita solicitada el '.$date
    		),
    		'field_groups' => array( 271 ),
    		'submit_value' => 'Solicitar cita'
    	);
    
    	acf_form( $options );

    This works for me.

    Sorry for my English.

    Hope you serve until the bug is corrected.

  • I updated the code from the first poster. You can’t check for is_admin, because it returns true (Ajax call is made to admin). See Documentation

    https://codex.wordpress.org/Function_Reference/is_admin

    //Custom redirected after creating a post
    function custom_redirect_function_name($post_id) {
    		$redirect = get_the_permalink($post_id);
    		wp_redirect($redirect);
    		exit;
    }
    add_action('acf/save_post', 'custom_redirect_function_name', 20);

    $redirect can be anything you want in my case it is as follow’s

    $redirect="/thank-you/?listing_id=$post_id"

    I wanted an url like this to process some further actions based on user choice (option to upgrade to different levels)

  • The is_admin portion works just fine for me so that when editing posts on the back-end it does not redirect to the front-end custom redirect URL.

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

The topic ‘FR: acf_form return to permalink’ is closed to new replies.