Last active
May 7, 2018 13:31
-
-
Save robincornett/a27aa6ce24d65cd9e230d85699a0da7e to your computer and use it in GitHub Desktop.
Change Scriptless Social Sharing links to use SVG icons instead of CSS pseudo elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.sss-name { | |
margin-left: 12px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get markup for an SVG icon. | |
* | |
* @param string $icon | |
* | |
* @param array $args | |
* | |
* @return string | |
*/ | |
function leaven_get_svg( $icon, $args = array() ) { | |
$defaults = array( | |
'title' => '', | |
'desc' => '', | |
'fallback' => false, | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$aria_hidden = ' aria-hidden="true"'; | |
$aria_labelledby = ''; | |
$title = ''; | |
$fallback = ''; | |
if ( $args['title'] ) { | |
$aria_hidden = ''; | |
$unique_id = uniqid(); | |
$aria_labelledby = ' aria-labelledby="title-' . $unique_id . '"'; | |
$title = sprintf( '<title id="title-%s">%s</title>', | |
$unique_id, | |
esc_html( $args['title'] ) | |
); | |
if ( $args['desc'] ) { | |
$aria_labelledby = ' aria-labelledby="title-' . $unique_id . ' desc-' . $unique_id . '"'; | |
$title .= sprintf( '<desc id="desc-%s">%s</desc>', | |
$unique_id, | |
esc_html( $args['desc'] ) | |
); | |
} | |
} | |
if ( $args['fallback'] ) { | |
$fallback = '<span class="svg-fallback icon-' . esc_attr( $icon ) . '"></span>'; | |
} | |
return sprintf( '<svg class="icon %1$s" role="img"%2$s%3$s>%4$s <use xlink:href="#%1$s"></use> %5$s</svg>', | |
esc_attr( $icon ), | |
$aria_hidden, | |
$aria_labelledby, | |
$title, | |
$fallback | |
); | |
} | |
add_filter( 'wp_kses_allowed_html', 'leaven_filter_allowed_html', 10, 2 ); | |
/** | |
* Allow SVG in wp_kses_post output. | |
* @param $allowed | |
* @param $context | |
* | |
* @return mixed | |
*/ | |
function leaven_filter_allowed_html( $allowed, $context ) { | |
if ( 'post' === $context ) { | |
$allowed['svg'] = array( | |
'class' => true, | |
); | |
$allowed['use'] = array( | |
'xlink:href' => true, | |
); | |
} | |
return $allowed; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment