Support

Account

Home Forums Front-end Issues How do I change the redirect after creating a new post via front-end?

Solved

How do I change the redirect after creating a new post via front-end?

  • Hello,

    I’m using the following code to create a new post using ACF on the front-end of my website. It’s currently redirecting me to http://mydomain.com/?post_id=127 . Because of my permalink settings, this is a 404 and leads nowhere (although 127 is the post ID). I’d like it to redirect to the http://mydomain.com/catalog/%post_slug% . How do I do that?

    The current code in my functions.php:

    // ACF create new item
    function my_pre_save_post( $post_id ) {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
        // Create a new post
        $post = array(
    			'post_type'     => 'catalog', // Your post type ( post, page, custom post type )
    			'post_status'   => 'publish', // (publish, draft, private, etc.)
    			'post_title'    => wp_strip_all_tags($_POST['fields']['field_5587be10d3660']), // Post Title ACF field key
    			'post_content'  => $_POST['fields']['field_5587c6e29c04e'], // Post Content ACF field key
        );
        // 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;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
  • Are you trying to redirect to the post that was just created?

    From the acf_form() documentation
    http://www.advancedcustomfields.com/resources/acf_form/

    
    /* (string) The URL to be redirected to after the form is submit. Defaults to the current URL with a GET parameter '?updated=true'.
    	A special placeholder '%post_url%' will be converted to post's permalink (handy if creating a new post) */
    'return' => '',
    
  • Yes I am. I’m not on ACF Pro, however (so I’m working with the limitations of ACF 4. Sorry for not mentioning that sooner.). So if I use

    'return' => '%post_url%',

    Then I get brought to this url: http://mydomain.com/%post_url%?post_id=127

    Which loads up as:

    Bad request!
    
    Your browser (or proxy) sent a request that this server could not understand.
    
    Error 400
  • Ah, I see, then you’re doing it right according to the ACF4 documentation

    but that still looks odd to me, why not get the permalink for the post you just created and set it to that? You’ll have to excuse my ignorance, I’ve never tried it before but it seems like this should work

    
    $url - get_permalink($post_id);
    $_POST['return'] = $url);
    

    but I could be wrong.

  • Ah, brilliant. Thank you! Not that experienced with code like this (and that original snippet I found off some tutorial somewhere).

    It worked, with some minor typo fixes in your code. Thanks again!

        $url = get_permalink($post_id);
    	$_POST['return'] = ($url);
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How do I change the redirect after creating a new post via front-end?’ is closed to new replies.