Can I edit the slug in an acf front end form?
I would like to do it in a similar way to editing the title in the font end form.
I was able to populate a custom field with get_post_field but I think there will be problem on the front end when the url changes and the page attempts to reload.
Would this be possible.
I believe you can create a dummy text field so you can input the slug you want and then add a function to change the post slug on save like this:
function my_acf_save_post( $post_id ) {
$new_slug = get_field('dummy_slug_custom_field');
$my_post = array(
'ID' => $post_id,
'post_name' => $new_slug,
);
remove_action('acf/save_post', 'my_acf_save_post', 20);
wp_update_post( $my_post );
add_action('acf/save_post', 'my_acf_save_post', 20);
wp_redirect( get_permalink($post_id) );
exit;
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
Where “dummy_slug_custom_field” is the dummy custom field for the post slug.
I hope this helps 🙂
Hi James
I’m using this code and it works, but only if i hit the Update button the second time, on the front end or in the backend. Do you know why it does not make any affect after the first save?
Update:
As it seems the get_field in this case need the post id as a second parameter:
get_field( 'dummy_slug_custom_field', $post_id );
is there any way to do it without a redirect. It’s kind of bothering.
The topic ‘Edit Slug in ACF Form’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.