admin管理员组文章数量:1334184
The Twenty Nineteen theme uses inline SVG for its icons instead of icon fonts. The icons are added to the HTML code with the TwentyNineteen_SVG_Icons::get_svg()
function.
public static function get_svg( $group, $icon, $size ) {
if ( 'ui' == $group ) {
$arr = self::$ui_icons;
} elseif ( 'social' == $group ) {
$arr = self::$social_icons;
} else {
$arr = array();
}
if ( array_key_exists( $icon, $arr ) ) {
$repl = sprintf( '<svg class="svg-icon" width="%d" height="%d" aria-hidden="true" role="img" focusable="false" ', $size, $size );
$svg = preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code.
$svg = preg_replace( "/([\n\t]+)/", ' ', $svg ); // Remove newlines & tabs.
$svg = preg_replace( '/>\s*</', '><', $svg ); // Remove white space between SVG tags.
return $svg;
}
return null;
}
When themes used icon fonts it was extremely easy to swap one icon for another with just a few lines of CSS code in a child theme. It seems that now with SVG icons one has to rewrite or override PHP functions just to add or swap one icon. Am I wrong? Is there an easy way to swap one SVG icon for another in a child theme of the Twenty Nineteen theme?
The Twenty Nineteen theme uses inline SVG for its icons instead of icon fonts. The icons are added to the HTML code with the TwentyNineteen_SVG_Icons::get_svg()
function.
public static function get_svg( $group, $icon, $size ) {
if ( 'ui' == $group ) {
$arr = self::$ui_icons;
} elseif ( 'social' == $group ) {
$arr = self::$social_icons;
} else {
$arr = array();
}
if ( array_key_exists( $icon, $arr ) ) {
$repl = sprintf( '<svg class="svg-icon" width="%d" height="%d" aria-hidden="true" role="img" focusable="false" ', $size, $size );
$svg = preg_replace( '/^<svg /', $repl, trim( $arr[ $icon ] ) ); // Add extra attributes to SVG code.
$svg = preg_replace( "/([\n\t]+)/", ' ', $svg ); // Remove newlines & tabs.
$svg = preg_replace( '/>\s*</', '><', $svg ); // Remove white space between SVG tags.
return $svg;
}
return null;
}
When themes used icon fonts it was extremely easy to swap one icon for another with just a few lines of CSS code in a child theme. It seems that now with SVG icons one has to rewrite or override PHP functions just to add or swap one icon. Am I wrong? Is there an easy way to swap one SVG icon for another in a child theme of the Twenty Nineteen theme?
Share Improve this question asked Apr 26, 2020 at 7:56 leemonleemon 2,0324 gold badges25 silver badges51 bronze badges1 Answer
Reset to default 0I see that both icon arrays ($ui_icons
and $social_icons
) in Twenty Nineteen can be accessed publicly, so I managed to replace any icon via the init
hook in a child theme. An example:
function child_init() {
TwentyNineteen_SVG_Icons::$social_icons['instagram'] = '<svg>... Insert new Instagram icon SVG code here ...</svg>';
}
add_action( 'init', 'child_init' );
本文标签: Swap SVG icons in child themes
版权声明:本文标题:Swap SVG icons in child themes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742365092a2461131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论