Support

Account

Home Forums General Issues ACF Form Delete Post

Solved

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:

    https://wordpress.stackexchange.com/questions/268809/allow-role-to-delete-posts-but-block-him-the-wp-admin

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘ACF Form Delete Post’ is closed to new replies.