Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created October 16, 2015 20:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/5e7285f7e8bc09871075 to your computer and use it in GitHub Desktop.
Save jchristopher/5e7285f7e8bc09871075 to your computer and use it in GitHub Desktop.
<?php
function iti_get_svg_markup( $svg_id = 0 ) {
// get the file name of the svg
$svg_id = absint( $svg_id );
$svg_file = get_post_meta( $svg_id, '_wp_attached_file', true );
// locate the file on disk
$upload_dir = wp_upload_dir();
$upload_basedir = trailingslashit( $upload_dir['basedir'] );
$svg_markup = iti_get_svg_content( $upload_basedir . $svg_file );
return $svg_markup;
}
function iti_get_svg_content( $svg_file = '' ) {
// prep the filesystem
global $wp_filesystem;
include_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
// make sure at least the extension is proper
if ( '.svg' !== strtolower( substr( $svg_file, -4 ) ) ) {
return '';
}
if ( ! file_exists( $svg_file ) ) {
return '';
}
// retrieve the markup
$svg_markup = $wp_filesystem->get_contents( $svg_file );
// filter the markup
$allowed = array(
'svg' => array(
'width' => array(),
'height' => array(),
'viewbox' => array(),
'version' => array(),
'xmlns' => array(),
'xmlns:xlink' => array(),
),
'g' => array(
'stroke' => array(),
'stroke-width' => array(),
// 'fill' => array(),
'fill-rule' => array(),
),
'path' => array(
'd' => array(),
'id' => array(),
),
);
return wp_kses( $svg_markup, $allowed );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment