-
Add grunt-svgstore to your build tool:
npm install grunt-svgstore --save-dev
-
Create an
svgstore
task in Grunt (liketasks/options/svgstore.js
):module.exports = { main: { files: { 'assets/svg/symbol-defs.svg': ['assets/svg/src/*.svg'] }, options: { cleanup: true, prefix : 'icon-' } } };
-
Leverage a shortcut function in PHP (like
svg_icon
):<?php function svg_icon( $name, $opts = array() ) { $href = WPTHEME_TEMPLATE_URL . '/assets/svg/symbol-defs.svg#icon-' . $name; $role = empty( $opts['role'] ) ? 'presentation' : $opts['role']; $class = empty( $opts['class'] ) ? 'icon icon-' . $name : 'icon ' . $opts['class']; $label = ' aria-label="' . ( empty( $opts['label'] ) ? $name : $opts['label'] ) . '"'; $label = $role === 'presentation' ? '' : $label; return sprintf( '<svg role="%s" class="%s"%s><use xlink:href="%s"/></svg>', $role, $class, $label, $href ); } // Usage: echo svg_icon( 'home' ) // echo svg_icon( 'home', array( 'class' => ' icon-home icon--active' ) ) // echo svg_icon( 'home', array( 'role' => 'img', 'label' = 'My home' ) ) ?>
-
Add support for Internet Explorer with SVG4Everybody:
<?php wp_enqueue_script( 'wptheme-svg4everybody', 'https://cdnjs.cloudflare.com/ajax/libs/svg4everybody/2.0.3/svg4everybody.js', array(), WPTHEME_VERSION, true ); // Add `svg4everybody()` to your theme JavaScript // Make your theme JavaScript depend on 'wptheme-svg4everybody' ?>
Last active
October 9, 2017 07:42
-
-
Save jonathantneal/293f2c6d2ec96a79d024 to your computer and use it in GitHub Desktop.
Using an SVG Icon System in WordPress with Grunt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The sprintf arguments should be escaped: