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.
Hi @frankster1234
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 🙂
Thanks a lot for the code block, worked perfectly!
@acf-support
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?
@acf-support
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.