• Is it possible to use wp_delete_attachment­
    and wp_delete_post to create a link or preferably a checkbox that you could use when’s editing a custom post type, so that when the post is deleted that all the images and attachments get permanently deleted as well?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter kentopolis

    (@kentopolis)

    Ok, I found this piece of code, and I know I should know how to do this… But how do I create a link that will call this function? It would be nice if it had a popup that warned the user it was a permanent delete option:

    // Delete all attatchements with post
    function remove_post() {
    
                if(isset($_POST['portfolio']) && is_numeric($_POST['portfolio'])) {
    
                    $post = get_post($_POST['portfolio']);
    
                    if(get_current_user_id() == $post->post_author) { 
    
                         $args = array(
                                 'post_parent' => $_POST['portfolio']
                     );
    
                     $post_attachments = get_children($args);
    
                         if($post_attachments) {
    
                                foreach ($post_attachments as $attachment) {
    
                                          wp_delete_attachment($attachment->ID, true);
    
                                }
    
                         }
    
                              wp_delete_post($_POST['portfolio'], true);
    
                    }
    
                }  
    
                exit;
    }

    You can use JavaScript to prompt you:

    Found this here – Example

    <script>
    function confirmDelete(delUrl) {
      if (confirm("Are you sure you want to delete")) {
       document.location = delUrl;
      }
    }
    </script>
    
    <a href="javascript:confirmDelete('delete.page?id=1')">Delete</a>

    Another way

    <a href="delete.page?id=1" onclick="return confirm('Are you sure you want to delete?')">Delete</a>

    Warning: This JavaScript will not stop the records from being deleted if they just navigate to the final url – delete.page?id=1 in their browser

    Thread Starter kentopolis

    (@kentopolis)

    does this delete.page?id=1 need to be the actual id of the page? Also, I’m having a hard time getting it to show up in a meta box…

    Thread Starter kentopolis

    (@kentopolis)

    I can’t get it to work, even by just going to the actual link

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Delete post and attachments at the same time’ is closed to new replies.