• Hi all,

    I am working on a plugin feature that checks for usage of image media before deletion – specifically within Advanced Custom Fields image fields in posts of a custom post type, but this part is not the issue.

    I can use a WP_Query/meta_query to successfully check for this scenario, but the problem is that I would like to print the results of my WP_Query to the wp-admin/upload.php page so that administrators can understand if the attachment was deleted successfully, or if not, why. I am not sure how to make this possible.

    Here is example code I am testing with now. Currently I am writing the results of my check to wp-content/debug.log. It would be better if administrators could see that in wp-admin.

    // Prevent deletion of attached media
    function check_acf_image_field_usage( $attachment_id ) {
    
        // Relevant meta keys are the image field names
        $image_field_names = array(
            'acf_image_field_name_example_1',
            'acf_image_field_name_example_2',
            'acf_image_field_name_example_3',
        );
    
        // Create a meta query from $image_field_names starting with 'relation' => 'OR'
        $image_fields_meta_query = array('relation' => 'OR');
        for ($i = 0; $i < count($image_field_names); $i++)  {
            $newArray = array(
                'key' => $image_field_names[$i],
                'value' => $attachment_id,
                'compare' => '=',
            );
            array_push($image_fields_meta_query, $newArray);
        }
    
        $args = array(
            'post_type'  => 'custom-post-type',
            'meta_query' => $image_fields_meta_query
        );
        $query = new WP_Query($args);
    
        // If 0 posts were found in the meta_query
        if (count($query->posts) === 0) {
            // CHANGE THIS write_log FUNCTION INTO SOMETHING THAT WILL OUTPUT TO wp-admin?
            write_log("The attachment that was requested to be deleted is included in " . count($query->posts) . " posts, so it will be deleted.");
    
            // Proceed to delete the attachment.
            return;
        
        // If 1 or more posts were found in the meta_query
        } else if (count($query->posts) > 0) {
            // CHANGE THESE write_log FUNCTIONS INTO SOMETHING THAT WILL OUTPUT TO wp-admin?
            write_log("The attachment that was requested to be deleted is included in " . count($query->posts) . " posts.");
            write_log('It is currently attached as an image to:');
    
            // Loop through meta query results to log each discovered post containing the attachment
            for ($i = 0; $i < count($query->posts); $i++)  {
                write_log($query->posts[$i]->ID . ' - ' . $query->posts[$i]->post_title);
            }
    
            // Stop WordPress execution so that the attachment is not deleted.
            $message = 'Sorry, this attachment cannot be deleted.'; 
            wp_die($message);
        }
    }
    add_action( 'delete_attachment', 'check_acf_image_field_usage' );
    
    // Generic function for writing data to wp-content/debug.log file
    function write_log( $log ) {
    
        if ( true === WP_DEBUG ) {
    
            if ( is_array($log) || is_object($log) ) {
                error_log( print_r($log, true) );
            } else {
                error_log( $log );
            }
            
        }
    
    }

    Here are the requirements:
    – Print results of a WP_Query to a wp-admin page (upload.php) that happens within the ‘delete_attachment’ hook.
    – All that matters to me is that administrators see the results of the query, I don’t care much about how or where it appears.
    – In other words, the content for the admin notification depends on the WP_Query results, which runs when an admin tries to delete an attachment.
    – What is the best way to print information from a WP_Query to a wp-admin page at the time the hook is called?
    – Simply using an admin notice doesn’t seem to be a solution because that shows the same data every time the page is loaded, and the WP_Query does not run when the page loads.

    Additionally, I am wondering is there a better method than using wp_die() to stop the attachment from being deleted?

    Thanks for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    What if you added a column to the list table. Each cell could list the posts where the related image is still in use? A lot to fit in one cell, maybe the cell is just a button that launches a modal showing the full list? And maybe include a button to delete anyway?

    How can your plugin be sure an image is not used outside of a posts context? Like used by a theme in header or footer content. Or is it beyond your plugin’s scope?

    Thread Starter jellygatorade

    (@jellygatorade)

    @bcworkz Adding a column to the list table isn’t a bad idea. I’ll have to look into how much of a lift that is. Before now, I haven’t done much in terms of adding features to the wp-admin pages (mostly removing them :P).

    Good question about being sure the image isn’t used outside of a posts context. It is beyond the scope of my project right now. The WordPress instance I’m working with does not support a traditional website, it publishes data to REST API only so this is not a concern.

    Thread Starter jellygatorade

    (@jellygatorade)

    @bcworkz Just writing back to say I implemented your idea of adding a column to the list table. Thanks!

    It was fairly easy to do following this guide:
    “How to Modify or Add Custom Columns to Post List in WordPress Admin”
    https://awhitepixel.com/blog/modify-add-custom-columns-post-list-wordpress-admin/

    And using the hooks ‘manage_media_columns’ and ‘manage_media_custom_column’

    This is a good enough workaround for me now, but if any others have another possible solution for this let me know!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Print results of WP_Query to wp-admin/upload.php within delete_attachment hook?’ is closed to new replies.