Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Created June 27, 2023 10:03
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 dkesberg/088a13b0bc20881b2e5b5016d32b9b2f to your computer and use it in GitHub Desktop.
Save dkesberg/088a13b0bc20881b2e5b5016d32b9b2f to your computer and use it in GitHub Desktop.
WP Sanitize SVG markup
/**
* Sanitize SVG markup for front-end display.
*
* @link https://developer.wordpress.org/reference/functions/wp_kses/#comment-6185
*
* @param string $svg SVG markup to sanitize.
* @return string Sanitized markup.
*/
function sanitize_svg( $svg = '' ) {
$allowed_html = array(
'svg' => array(
'xmlns' => array(),
'fill' => array(),
'viewbox' => array(),
'role' => array(),
'aria-hidden' => array(),
'focusable' => array(),
'height' => array(),
'width' => array(),
),
'path' => array(
'd' => array(),
'fill' => array(),
),
);
return wp_kses( $svg, $allowed_html );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment