eddev
Snippets

SVGs

Copy/paste SVG upload and GraphQL snippets

Expose Raw SVG Content

Use a computed GraphQL field when the frontend needs the raw SVG string instead of a media URL.

add_action('graphql_register_types', function () {
  register_graphql_field('MediaItem', 'svgContent', [
    'type' => 'String',
    'description' => __('The raw SVG content of the media item', 'your-textdomain'),
    'resolve' => function ($mediaItem) {
      $filePath = get_attached_file($mediaItem->ID);

      if (preg_match('/\.svg$/i', $filePath) && file_exists($filePath)) {
        return file_get_contents($filePath);
      }

      return null;
    },
  ]);
});
query IconBlock {
  block {
    content_icon {
      icon {
        svgContent
        altText
      }
    }
  }
}

On this page