Skip to content

Instantly share code, notes, and snippets.

@webarthur
Created March 10, 2021 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webarthur/15320c52e3d90597a557125b5a968fe5 to your computer and use it in GitHub Desktop.
Save webarthur/15320c52e3d90597a557125b5a968fe5 to your computer and use it in GitHub Desktop.
Function to replace SVG images to inline SVG code (WordPress)
<?php
function wp_svg_inline_replacer($matches) {
$src = $matches[1];
$parts = explode('/wp-content/', $src);
$svg = file_get_contents(WP_CONTENT_DIR . '/' . $parts[1]);
$svg = preg_replace('/<\?xml.*\?>/', '', $svg);
$svg = preg_replace('/<\!DOCTYPE[^>]*>/', '', $svg);
return trim($svg);
}
function wp_svg_inline_filter($content) {
global $post;
$pattern = "/<img.*src=\"(.*\.svg)\"(.*) \/>/i";
$content = preg_replace_callback($pattern, 'wp_svg_inline_replacer', $content);
return $content;
}
add_filter('the_content','wp_svg_inline_filter');
add_filter('get_custom_logo','wp_svg_inline_filter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment