• Resolved Rasso Hilber

    (@nonverbla)


    Basically the title says it all – I have a few ACF options pages where I also have some image fields. These images are detected as “not used” by Media Cleaner. I can of course make use of the filter wpmc_check_media and return true for these images, but still thought it would be worth informing you about the issue.

    Should you decide that you want to support this: You can get information about all registered options pages like this:

    add_action('init', function() {
        $options_pages = acf_get_options_pages();
        // do your magic with the information
    }, PHP_INT_MAX); // run at the very end of the 'init' hook

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Rasso Hilber

    (@nonverbla)

    As a temporary workaround I developed this filter callback:

    /**
     * Tells Media Cleaner Pro if an image is in use on any ACF options page
     * @see https://meowapps.com/media-cleaner/faq/#media-library-method
     *
     * @param bool $is_used     What WPMC thinks about the attachment
     * @param int $id           The attachment ID
     */
    function check_if_media_is_in_use_on_acf_options_pages(bool $is_used, int $id): bool
    {
        $options_pages = acf_get_options_pages();
        if (empty($options_pages)) return $is_used;
    
        // This is what we will be looking for:
        $serialized_id = serialize((string) $id);
    
        // Look through all options pages
        foreach ($options_pages as $options_page) {
            // Get a serialized array of all fields, unformatted
            $serialized_fields = serialize(get_fields($options_page['post_id'], false) ?: []);
            // Check if the serialized $id can be found in the serialized fields
            if (str_contains($serialized_fields, $serialized_id)) return true;
        }
    
        return $is_used;
    }
    add_filter('wpmc_check_media', 'check_if_media_is_in_use_on_acf_options_pages', 10, 2);

    To support all possible scenarios (single image/file, gallery, repeaters, flexible content,…) I opted to just serialize all fields and check if the serialized version of the $id can be found there. Works smoothly!

    Plugin Author Jordy Meow

    (@tigroumeow)

    Thanks a lot ?? I’ll reply to you in private.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Media Cleaner doesn’t detect usage of attachments on ACF options pages’ is closed to new replies.