Home › Forums › Front-end Issues › 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);
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
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.