Skip to content

Bug Report: WordPress Plugin ignores “Enable SVG support” for down-synced Cloudinary assets (imports PNG) #1087

Closed

Description

@TheRealFreelancer

Bug Report: WordPress Plugin ignores “Enable SVG support” for down-synced Cloudinary assets (imports PNG)

Environment

  • Cloudinary WordPress Plugin: 3.2.12
  • WordPress: latest
  • SVG support toggle in plugin settings: enabled

Steps to Reproduce

  1. Upload an SVG file directly into Cloudinary (not via WordPress).
    • Asset is stored as SVG in Cloudinary, no transforms/presets applied.
  2. In WordPress, go to Cloudinary → Add from Cloudinary and import that SVG asset.
  3. The imported attachment in WordPress Media Library is registered as PNG (.png URL, MIME image/png).
  4. If you upload the same SVG via WordPress, the plugin correctly stores it in Cloudinary and registers it as SVG (.svg URL, MIME image/svg+xml).

Expected Behavior

With Enable SVG Support active, importing an existing Cloudinary SVG into WordPress should preserve it as SVG (URL ending .svg, MIME image/svg+xml).


Actual Behavior

For down-synced/imported assets, the plugin treats SVG as “convertible” and replaces it with a PNG variant.


Technical Notes

  • In php/class-media.php, the method get_convertible_extensions() includes:
    'svg' => 'png',
  • This conversion logic runs during import/sync and overrides the SVG support logic in php/class-svg.php.
  • As a result, “Enable SVG support” is ignored for the down-sync/import path.

Workaround

Adding these filters in a MU-plugin fixes the behavior:

// Prevent SVG → PNG conversion
add_filter('cloudinary_convert_media_types', function ($types) {
    unset($types['svg']);
    return $types;
});

// Force resource_type raw for SVG
add_filter('cloudinary_resource_type', function ($type, $id) {
    if (get_post_mime_type($id) === 'image/svg+xml') return 'raw';
    return $type;
}, 10, 2);

// Disable eager formats for SVG
add_filter('cloudinary_upload_eager_formats', function ($formats, $id) {
    if (get_post_mime_type($id) === 'image/svg+xml') return [];
    return $formats;
}, 10, 2);

Suggested Fix

  • If SVG support is enabled, remove 'svg' => 'png' from get_convertible_extensions().
  • Ensure down-sync/import logic respects SVG support and sets resource_type = raw for SVG assets.
  • Automatically disable eager transformations for SVG.

Impact: Without this fix, SVG support only works for assets uploaded via WordPress, not for assets that already exist in Cloudinary.

Activity

  1. TheRealFreelancer commented on Aug 26, 2025

    @TheRealFreelancer
    Author

    Update: Working MU-Plugin Fix (v2)

    After further testing I found that forcing resource_type=raw caused SVGs not to render inline in WordPress/Elementor (URLs like /raw/upload/.../icon.svg would not display in <img> tags).

    The proper delivery path is via image/upload with f_svg,fl_sanitize and a .svg extension.
    This way the browser/WordPress Media Library/Elementor all render the SVG correctly, while PNG-conversion is prevented.


    Confirmed Fix (MU-Plugin)

    <?php
    /**
     * Plugin Name: Cloudinary SVG Fix (MU) v2
     * Description: Prevents PNG conversion of SVGs and enforces proper SVG delivery via image/upload + f_svg,fl_sanitize.
     * Version: 2.0.0
     */
    
    // Remove SVG → PNG conversion mapping.
    add_filter('cloudinary_convert_media_types', function ($types) {
        if (isset($types['svg'])) unset($types['svg']);
        return $types;
    });
    
    // Disable eager formats for SVG.
    add_filter('cloudinary_upload_eager_formats', function ($formats, $attachment_id) {
        if (get_post_mime_type($attachment_id) === 'image/svg+xml') return [];
        return $formats;
    }, 10, 2);
    
    // Allow SVG uploads.
    add_filter('upload_mimes', function ($m) {
        $m['svg']  = 'image/svg+xml';
        $m['svgz'] = 'image/svg+xml';
        return $m;
    }, 10, 1);
    
    // Normalize SVG delivery URLs to image/upload with f_svg,fl_sanitize and .svg extension.
    function cld_fix_svg_url($url){
        if (!$url || strpos($url, 'res.cloudinary.com') === false) return $url;
        $url = preg_replace('#/raw/upload/#i', '/image/upload/', $url);
        if (preg_match('#^(https?://[^/]+/image/upload/)(.*)$#i', $url, $m)) {
            $base = $m[1];
            $tail = $m[2];
            $transforms = '';
            $version_and_path = $tail;
            if (preg_match('#^([^/]+)/(v\d+/.*)$#', $tail, $t)) {
                $transforms = $t[1];
                $version_and_path = $t[2];
            }
            $tlist = array_filter(array_map('trim', $transforms ? explode(',', $transforms) : []));
            if (stripos($transforms, 'f_svg') === false) $tlist[] = 'f_svg';
            if (stripos($transforms, 'fl_sanitize') === false) $tlist[] = 'fl_sanitize';
            $transforms_final = implode(',', array_unique($tlist));
            $version_and_path = preg_replace('#\.(png|jpg|jpeg|webp)(\?.*)?$#i', '.svg$2', $version_and_path);
            if (!preg_match('#\.svg(\?.*)?$#i', $version_and_path)) {
                $version_and_path = preg_replace('#($|\?)#', '.svg$1', $version_and_path, 1);
            }
            $new = $base;
            if ($transforms_final) $new .= $transforms_final . '/';
            $new .= $version_and_path;
            return $new;
        }
        return preg_replace('#\.png(\?.*)?$#i', '.svg$1', $url);
    }
    add_filter('wp_get_attachment_image_src', function($img, $id){
        if (is_array($img) && get_post_mime_type($id)==='image/svg+xml') $img[0]=cld_fix_svg_url($img[0]);
        return $img;
    },10,2);
    add_filter('wp_get_attachment_url', function($url, $id){
        if (get_post_mime_type($id)==='image/svg+xml') return cld_fix_svg_url($url);
        return $url;
    },10,2);

    Summary

    • The root issue is that 'svg' => 'png' mapping in get_convertible_extensions() still applies even with SVG support enabled.
    • Uploads from WP → CLD work fine, but down-sync/import from CLD → WP imports as PNG.
    • Forcing raw breaks inline rendering.
    • The above fix keeps SVGs as SVGs and renders correctly everywhere.

    Recommendation:

    • When Enable SVG support is active, the plugin should:
      • Skip 'svg' => 'png' conversion entirely.
      • Deliver SVGs via image/upload with f_svg,fl_sanitize and .svg extension.
      • Disable eager transforms for SVG automatically.

    This would make SVG support consistent and remove the need for custom MU-plugins.

  2. Vdeub-cloudinary commented on Aug 27, 2025

    @Vdeub-cloudinary

    Hi @TheRealFreelancer, thanks for opening this bug and providing a mu-plugin.
    I was also able to reproduce the issue on my side and I have reached out to the team to look at your solution.
    We'll keep you posted on this!

  3. TheRealFreelancer commented on Aug 28, 2025

    @TheRealFreelancer
    Author

    Hi @Vdeub-cloudinary, thank you for reaching out to the team. I'm glad you could reproduce the bug. Were you be able to also (temporarily) patch it with the mu-plugin? Regards

  4. aatanasov-cloudinary commented on Aug 30, 2025

    @aatanasov-cloudinary
    Contributor

    Hi @TheRealFreelancer, thank you for reporting this issue.

    We applied a general fix to the plugin, but it will be released in the next version.

    Would you be able to test this fix on your side https://github.com/cloudinary/cloudinary_wordpress/pull/1088/files and confirm it works for your case (without using the mu-plugin)? You can modify the Cloudinary plugin temporarily for the test.

    This is the PR: #1088

    Thanks!

  5. TheRealFreelancer commented on Aug 30, 2025

    @TheRealFreelancer
    Author

    Hi @aatanasov-cloudinary, thank you for taking action on this matter.

    After modifying class-svg.php with your fix, I can confirm that it resolves the issue. Much appreciated.

    Behavior:

    • SVG Support Off: Import SVG -> Converts to PNG
    • SVG Support On: Import SVG -> Remains in SVG format

    Just a note: Turning off SVG Support after importing an SVG renders the imported file unusable (in a "Cloudinary Only" storage scenario). I'm not sure if this is intentional, but if someone deletes the "deactivated" SVG file, it removes the main file in Cloudinary. This could be a bit confusing. Maybe the user experience could be improved with that.

    Image Image
  6. Vdeub-cloudinary commented on Sep 17, 2025

    @Vdeub-cloudinary

    @TheRealFreelancer, we have just released 3.2.13 which embeds the fix for this issue.

  7. TheRealFreelancer commented on Sep 21, 2025

    @TheRealFreelancer
    Author

    I can confirm that the issue is resolved now.

  8. aatanasov-cloudinary commented on Oct 9, 2025

    @aatanasov-cloudinary
    Contributor

    Hi @TheRealFreelancer , thanks for confirming. Closing this issue.

    Regarding:

    Just a note: Turning off SVG Support after importing an SVG renders the imported file unusable (in a "Cloudinary Only" storage scenario).

    We discussed this internally, and that behaviour is expected, and we won't apply changes to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions