• Resolved mehdimoradi7172

    (@mehdimoradi7172)


    hi

    thank you for ur great plugin, i need to see some cool features in ASE.

    • Delete all images of a post or product or CPT after deleting the post/product. at the moment the images remain in the database and bloat it. after the post was deleted, all images (featured image, gallery and images in the content) should be deleted
    • wordpress and woocommerce makes different image sizes like medium, medium large, single 600 and etc, being able to disable them will be a great option and let us decide which one should be generated.

    note: please at this features to the free version

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Bowo

    (@qriouslad)

    What are you using/doing to achieve the first one? i.e. delete images as you delete a post.

    The second one has been requested as well, most likely will go into the Pro version. Please understand that the free version is already generous enough with 40+ modules.

    Thread Starter mehdimoradi7172

    (@mehdimoradi7172)

    For both i use a code from chat gpt.

    1- code to delete images:

    // Hook into post deletion
    add_action('before_delete_post', 'delete_related_images_bulk');
    
    function delete_related_images_bulk($post_id) {
        static $all_attachment_ids = array(); // Store all attachment IDs
    
        if (function_exists('is_product') && is_product($post_id)) {
            // If it's a WooCommerce product, get the product gallery attachments
            $product = wc_get_product($post_id);
            $attachment_ids = $product->get_gallery_image_ids();
        } else {
            // For regular posts, get attached images including featured image
            $attachment_ids = get_attached_media('image', $post_id);
            $featured_image_id = get_post_thumbnail_id($post_id); // Get the featured image ID
            if ($featured_image_id) {
                $attachment_ids[] = $featured_image_id; // Add the featured image to the array
            }
        }
    
        if (!empty($attachment_ids)) {
            $all_attachment_ids = array_merge($all_attachment_ids, $attachment_ids);
        }
    
        // Final deletion action after all posts are processed
        add_action('delete_post', function() use ($all_attachment_ids) {
            foreach ($all_attachment_ids as $attachment_id) {
                // Check if the attachment is used in other posts or products
                $usage_count = count(get_posts(array('post_type' => array('post', 'product'), 'post_status' => 'any', 'meta_query' => array(array('key' => '_thumbnail_id', 'value' => $attachment_id)))));
    
                if ($usage_count <= 1) { // If used only in the current post/product
                    wp_delete_attachment($attachment_id, true); // Delete the attachment
                }
            }
        });
    }
    
    

    Plugin Author Bowo

    (@qriouslad)

    Thanks for the code snippets. Great that it also has a check to see if an image is used in another post or not.

    Thread Starter mehdimoradi7172

    (@mehdimoradi7172)

    Yes, hope to see it in your plugin 🌹🙏

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Feature request’ is closed to new replies.