Skip to content

Compatibility issue with Spectra Pro and temporary fix #158

Description

@Bokis

"Load more" in Global Media tab doesn't work if Spectra Pro is activated with PHP >8.
This is actually a Spectra problem, but here is a fix you can try if you get "Fatal error" after clicking on "Load more".

Create the file /wp-content/mu-plugins/fix-spectra-global-media.php and add following:

<?php
/**
 * Plugin Name: Fix Spectra Pro + Global Media AJAX Fatal
 * Description: Prevents Spectra Pro from crashing media AJAX when attachment path is empty.
 * Author: Internal
 */

add_action('plugins_loaded', function () {

    // Only run in admin
    if (!is_admin()) {
        return;
    }

    global $wp_filter;

    // Check if Spectra filter exists
    if (isset($wp_filter['wp_prepare_attachment_for_js'])) {

        foreach ($wp_filter['wp_prepare_attachment_for_js']->callbacks as $priority => $callbacks) {

            foreach ($callbacks as $callback_id => $callback_data) {

                if (
                    isset($callback_data['function']) &&
                    is_array($callback_data['function']) &&
                    is_object($callback_data['function'][0]) &&
                    get_class($callback_data['function'][0]) === 'SpectraPro\Core\Admin'
                ) {

                    $original_callback = $callback_data['function'];

                    // Remove original Spectra filter
                    remove_filter(
                        'wp_prepare_attachment_for_js',
                        $original_callback,
                        $priority
                    );

                    // Add safe wrapper
                    add_filter(
                        'wp_prepare_attachment_for_js',
                        function ($response, $attachment, $meta) use ($original_callback) {

                            try {
                                return call_user_func(
                                    $original_callback,
                                    $response,
                                    $attachment,
                                    $meta
                                );
                            } catch (ValueError $e) {
                                // Fail silently and return original response
                                return $response;
                            }

                        },
                        $priority,
                        3
                    );
                }
            }
        }
    }

}, 100);

What this does:

  • Locates Spectra Pro’s filter on wp_prepare_attachment_for_js
  • Removes it
  • Re-adds it inside a try/catch wrapper
  • Catches the ValueError thrown by file_get_contents('')
  • Returns a normal media response instead of a 500 error

Thanks ChatGPT!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions