Home › Forums › General Issues › ACF Form Delete Post
I have a site where users are able to upload galleries of their design projects. I’ve successfully used acf_form to allow the user to submit, and also edit these projects. However, what if they want to delete the project? I can’t find any documentation on whether it’s possible to allow the user to delete their projects from the front-end.
Giving people the ability to delete projects, even their own, could be dangerous.
You could add a true/false field “Delete This post”, then in your acf/save_post filter you could check this field, it it’s set to true then call wp_delete_post() https://developer.wordpress.org/reference/functions/wp_delete_post/
Maybe add a second field as well that has conditional logic to show if the first one is checked “Yes I’m sure I want to delete this”, maybe with some kind of warning that the action cannot be undone.
This site is a community site so there is no way I can have them upload content and not give them the ability to delete it if they want to. If there is someone that can write a sample function for this, that would be helpful. I don’t know what to do with the resource provided.
add_filter('acf/save_post', 'possibly_delete_post');
function possibly_delete_post($post_id) {
$post_type = get_post_type($post_id);
// change to post type you want them to be able to delete
if ($post_type != 'post-type') {
return;
}
if (get_field('delete_this_post', $post_id)) {
$force_delete = false; // move to trash
// change above to true to permanently delete
wp_delete_post($post_id, $force_delete);
// redirect them somewhere since the post will no longer exist
// see https://developer.wordpress.org/reference/functions/wp_redirect/
wp_redirect('/some_page/');
exit;
}
}
I’ve been looking into this lately as well (how to allow a user to delete on the frontend without having wp-admin access).
The ACF method works well, but also wanted to point out this:
You can use the normal WP delete post function, which requires wp-admin access, but redirect them away just after the delete function runs. So they never see wp-admin, but are still able to have the functions run.
Hopefully this is helpful to someone, as it was to me.
The topic ‘ACF Form Delete Post’ 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.