{"id":3355,"date":"2016-01-07T20:42:53","date_gmt":"2016-01-07T19:42:53","guid":{"rendered":"https:\/\/en.florianbrinkmann.de\/?p=3355"},"modified":"2020-02-09T11:00:01","modified_gmt":"2020-02-09T10:00:01","slug":"creating-a-social-icons-menu-with-svgs-in-wordpress","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/creating-a-social-icons-menu-with-svgs-in-wordpress-3355\/","title":{"rendered":"Creating a social icons menu with SVGs in WordPress"},"content":{"rendered":"\n<p>For my latest WordPress theme, I modified the output of a standard WordPress menu so that at the frontend URLs to social media channels are displayed as SVG icons. In this post, I show you my solution.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"update-box\">\n<p><strong>Update from February 14, 2016<\/strong>: I recently tested the menu with a screen reader and noticed, that the SVG accessibility features are not very well supported. Because of that, I updated the post\u2019s content.<\/p>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Why SVG and not the easy to use icon fonts?<\/h2>\n\n\n\n<p>Icon fonts have some disadvantages in comparison to SVGs \u2014 not only, that they are sometimes rendered incorrectly. SVGs are more semantic and accessible than icon fonts. <a href=\"https:\/\/twitter.com\/SaraSoueidan\">Sara Soueidan<\/a> wrote a chapter about SVGs in the Smashing Book 5, this is one paragraph from it:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u00bbSVGs are semantic. An SVG icon is a graphic \u2014 an image \u2014 so what better way to mark up an image than to use a (scalable vector) graphic tag? An SVG element (<code class=\"lang-markup\">&lt;svg&gt;<\/code>) represents an icon simply and semantically, while icon fonts usually require non-semantic markup like pseudo-elements and empty s to be displayed. For people concerned about semantics and accessibility this introduces a serious issue: these elements don\u2019t accommodate screen readers well, not to mention that the markup generally just doesn\u2019t make much sense \u2014 if any \u2014 for an image.\u00ab<\/p><\/blockquote>\n\n\n\n<p>For this reason, I did not want to use an icon font, but SVGs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution for the SVG menu: a custom <code class=\"lang-php\">Walker_Nav_Menu<\/code> class<\/h2>\n\n\n\n<p>The SVG file with the social media icons looks like that:<\/p>\n\n\n<pre class=\"wp-block-code lang-markup\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">svg<\/span> <span class=\"hljs-attr\">version<\/span>=<span class=\"hljs-string\">\"1.1\"<\/span> <span class=\"hljs-attr\">xmlns<\/span>=<span class=\"hljs-string\">\"http:\/\/www.w3.org\/2000\/svg\"<\/span> <span class=\"hljs-attr\">style<\/span>=<span class=\"hljs-string\">\"display: none;\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">symbol<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"icon-twitter\"<\/span> <span class=\"hljs-attr\">viewBox<\/span>=<span class=\"hljs-string\">\"0 0 16 16\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">path<\/span> <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M15,3.627c-0.515,0.229-1.068,0.383-1.649,0.452c0.592-0.355,1.048-0.918,1.262-1.589\n\tc-0.554,0.329-1.169,0.568-1.822,0.697c-0.524-0.558-1.271-0.907-2.098-0.907c-1.586,0-2.872,1.286-2.872,2.872\n\tc0,0.225,0.026,0.444,0.075,0.654c-2.387-0.12-4.504-1.263-5.921-3.001C1.728,3.229,1.586,3.724,1.586,4.25\n\tc0,0.996,0.506,1.875,1.277,2.39C2.393,6.625,1.95,6.496,1.562,6.281c0,0.013,0,0.024,0,0.037c0,1.392,0.989,2.552,2.304,2.816\n\tC3.625,9.199,3.372,9.234,3.11,9.234c-0.186,0-0.365-0.02-0.541-0.051c0.366,1.142,1.427,1.971,2.683,1.993\n\tc-0.982,0.771-2.222,1.23-3.566,1.23c-0.232,0-0.461-0.014-0.686-0.041c1.271,0.816,2.78,1.291,4.402,1.291\n\tc5.284,0,8.174-4.377,8.174-8.172c0-0.124-0.004-0.248-0.01-0.371C14.127,4.708,14.615,4.203,15,3.627z\"<\/span>\/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">symbol<\/span>&gt;<\/span>\n\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">symbol<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"icon-feed\"<\/span> <span class=\"hljs-attr\">viewBox<\/span>=<span class=\"hljs-string\">\"0 0 16 16\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">path<\/span> <span class=\"hljs-attr\">d<\/span>=<span class=\"hljs-string\">\"M2,6v2c3.309,0,6,2.691,6,6h2C10,9.582,6.418,6,2,6z M2,2v2c5.514,0,10,4.486,10,10h2\n\t\tC14,7.373,8.627,2,2,2z M3.5,11C2.671,11,2,11.672,2,12.5S2.671,14,3.5,14S5,13.328,5,12.5S4.329,11,3.5,11z\"<\/span>\/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">symbol<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">svg<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Theoretically, there is the possibility to insert a <code class=\"lang-markup\">title<\/code> element inside the <code class=\"lang-markup\">svg<\/code> \u2014 for example, to provide a title for screen reader users. That does not work in many combinations of screen readers and browsers. To provide an accessible title for the icons, we use a <code class=\"lang-markup\">span<\/code> element which is hidden via CSS. The desired SVG icon is shown with the help of the <code class=\"lang-markup\">use<\/code> element.<\/p>\n\n\n<pre class=\"wp-block-code lang-markup\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">svg<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"icon-twitter\"<\/span>&gt;<\/span>\n   <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">use<\/span> <span class=\"hljs-attr\">xlink:href<\/span>=<span class=\"hljs-string\">\"\/pfad-zum-theme\/svg\/social-media-icons.svg#icon-twitter\"<\/span>\/&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">svg<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">span<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"screen-reader-text\"<\/span>&gt;<\/span>Twitter<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">span<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The hash value at the end of the file path matches the ID of the icon\u2019s <code class=\"lang-markup\">symbol<\/code> element. So we need this code with the right hash value inside each menu item. To modify the output of <code class=\"lang-php\">wp_nav_menu()<\/code>, we can specify a Walker class.<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> ( has_nav_menu( <span class=\"hljs-string\">'social'<\/span> ) ) {\n   wp_nav_menu(\n      <span class=\"hljs-keyword\">array<\/span>(\n         <span class=\"hljs-string\">'theme_location'<\/span> =&gt; <span class=\"hljs-string\">'social'<\/span>,\n         <span class=\"hljs-string\">'menu_class'<\/span>     =&gt; <span class=\"hljs-string\">'social-menu'<\/span>,\n         <span class=\"hljs-string\">'container'<\/span>      =&gt; <span class=\"hljs-string\">''<\/span>,\n         <span class=\"hljs-string\">'walker'<\/span>         =&gt; <span class=\"hljs-keyword\">new<\/span> Svg_Social_Menu_Walker(),\n         <span class=\"hljs-string\">'depth'<\/span>          =&gt; <span class=\"hljs-number\">1<\/span>\n      )\n   );\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We will create a child class of <code class=\"lang-php\">Walker_Nav_Menu<\/code> because we only need to modify its <code class=\"lang-php\">start_el()<\/code> method. This method controls the output of menu items. We can copy large parts of this method, and the following code shows only the modified parts with copied code lines for reference.<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Svg_Social_Menu_Walker<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title\">Walker_Nav_Menu<\/span> <\/span>{\n   <span class=\"hljs-comment\">\/\/ &#91;\u2026]<\/span>\n   <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">start_el<\/span><span class=\"hljs-params\">( &amp;$output, $item, $depth = <span class=\"hljs-number\">0<\/span>,\n      $args = array<span class=\"hljs-params\">()<\/span>, $id = <span class=\"hljs-number\">0<\/span> )<\/span> <\/span>{\n      <span class=\"hljs-comment\">\/\/ &#91;\u2026]<\/span>\n      $title = apply_filters( <span class=\"hljs-string\">'nav_menu_item_title'<\/span>, $title, $item, $args, $depth );\n\n      $social_media_channels = <span class=\"hljs-keyword\">array<\/span>(\n         <span class=\"hljs-string\">'plus.google.com'<\/span> =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-google-plus'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Google Plus'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'wordpress.org'<\/span>   =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-wordpress'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'WordPress.org'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'wordpress.com'<\/span>   =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-wordpress'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'WordPress.com'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'facebook.com'<\/span>    =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-facebook'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Facebook'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'twitter.com'<\/span>     =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-twitter'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Twitter'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'dribbble.com'<\/span>    =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-dribbble'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Dribbble'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'pinterest.com'<\/span>   =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-pinterest'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Pinterest'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'github.com'<\/span>      =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-github'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'GitHub'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'tumblr.com'<\/span>      =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-tumblr'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Tumblr'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'youtube.com'<\/span>     =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-youtube'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'YouTube'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'flickr.com'<\/span>      =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-flickr'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Flickr'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'vimeo.com'<\/span>       =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-vimeo'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Vimeo'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'instagram.com'<\/span>   =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-instagram'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Instagram'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'linkedin.com'<\/span>    =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-linkedin'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'LinkedIn'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'xing.de'<\/span>         =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-xing'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Xing'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'xing.com'<\/span>        =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-xing'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Xing'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n         <span class=\"hljs-string\">'\/feed'<\/span>           =&gt; <span class=\"hljs-keyword\">array<\/span>(\n            <span class=\"hljs-string\">'id'<\/span>                 =&gt; <span class=\"hljs-string\">'icon-feed'<\/span>,\n            <span class=\"hljs-string\">'screen-reader-text'<\/span> =&gt; __( <span class=\"hljs-string\">'Feed'<\/span>, <span class=\"hljs-string\">'hannover'<\/span> )\n         ),\n      );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Own code begins at <code class=\"lang-php\">$social_media_channels<\/code>. Here we have to connect the URLs of the social networks with the IDs from the SVG file and the screen reader titles. We create an array of channel URL keys with value arrays, which include the respective ID and screen reader text for this channel.<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$svg_id = <span class=\"hljs-string\">\"\"<\/span>;\n\n<span class=\"hljs-keyword\">foreach<\/span> ( $social_media_channels <span class=\"hljs-keyword\">as<\/span> $key =&gt; $value ) {\n   $pattern = <span class=\"hljs-string\">\"|$key|\"<\/span>;\n   preg_match( $pattern, $atts&#91;<span class=\"hljs-string\">'href'<\/span>], $matches );\n   <span class=\"hljs-keyword\">if<\/span> ( ! <span class=\"hljs-keyword\">empty<\/span>( $matches&#91;<span class=\"hljs-number\">0<\/span>] ) ) {\n      $match                  = $matches&#91;<span class=\"hljs-number\">0<\/span>];\n      $svg_id                 = $social_media_channels&#91; $match ]&#91;<span class=\"hljs-string\">'id'<\/span>];\n      $svg_screen_reader_text = $social_media_channels&#91; $match ]&#91;<span class=\"hljs-string\">'screen-reader-text'<\/span>];\n      <span class=\"hljs-keyword\">break<\/span>;\n   }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the next step, we save an empty string in a variable <code class=\"lang-php\">$svg_id<\/code> and loop through the channel array. Inside the <code class=\"lang-php\">foreach<\/code> loop, we can access the current array key with <code class=\"lang-php\">$key<\/code> and the respective value (the array with ID and screen reader text) with <code class=\"lang-php\">$value<\/code>. <code class=\"lang-php\">$atts['href']<\/code> holds the URL of the current menu item. We need to compare this value with the array key, so we save the pattern <code class=\"lang-php\">|$key|<\/code>.<\/p>\n\n\n\n<p>After that, we use <code class=\"lang-php\">preg_match()<\/code> and pass the pattern, the menu item\u2019s URL and a variable for matches as parameters. If <code class=\"lang-php\">$matches[0]<\/code> is not empty, the current array key matches a part of the menu item\u2019s URL. The value of this array position is <code class=\"lang-php\">$key<\/code>, so the URL of the social media channel. If there is a match, we save this URL from <code class=\"lang-php\">$matches[0]<\/code> in <code class=\"lang-php\">$match<\/code> and fetch the ID from <code class=\"lang-php\">$social_media_channels[ $match ]['id'];<\/code> and the screen reader text from <code class=\"lang-php\">$social_media_channels[ $match ]['screen-reader-text'];<\/code>. After that, we break the loop.<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> ( $svg_id != <span class=\"hljs-string\">\"\"<\/span> ) {\n   $icon_url    = plugins_url( <span class=\"hljs-string\">\"svg\/social-media-icons.svg#$svg_id\"<\/span>, <span class=\"hljs-keyword\">__DIR__<\/span> );\n   $item_output = $args-&gt;before;\n   $item_output .= <span class=\"hljs-string\">'&lt;a'<\/span> . $attributes . <span class=\"hljs-string\">'&gt;'<\/span>;\n   $item_output .= <span class=\"hljs-string\">'&lt;svg class=\"'<\/span> . $svg_id . <span class=\"hljs-string\">'\"&gt;&lt;use xlink:href=\"'<\/span> . $icon_url . <span class=\"hljs-string\">'\"&gt;&lt;\/use&gt;&lt;\/svg&gt;&lt;span class=\"screen-reader-text\"&gt;'<\/span> . $svg_screen_reader_text . <span class=\"hljs-string\">'&lt;\/span&gt;'<\/span>;\n   $item_output .= <span class=\"hljs-string\">'&lt;\/a&gt;'<\/span>;\n   $item_output .= $args-&gt;after;\n} <span class=\"hljs-keyword\">else<\/span> {\n   $item_output = $args-&gt;before;\n   $item_output .= <span class=\"hljs-string\">'&lt;a'<\/span> . $attributes . <span class=\"hljs-string\">'&gt;'<\/span>;\n   $item_output .= $args-&gt;link_before . $title . $args-&gt;link_after;\n   $item_output .= <span class=\"hljs-string\">'&lt;\/a&gt;'<\/span>;\n   $item_output .= $args-&gt;after;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, we have to build the markup. If <code class=\"lang-php\">$svg_id<\/code> is not empty, we can display an SVG. In this case, we store the URL from the SVG file in <code class=\"lang-php\">$icon_url<\/code> and append the ID after a <code class=\"lang-markup\">#<\/code>. The following two lines are copied from the parent class. In the next line, we create an <code class=\"lang-markup\">svg<\/code> element with the ID as a class, so it can be styled. As the <code class=\"lang-markup\">href<\/code> attribute of the <code class=\"lang-markup\">use<\/code> element, we pass the URL and close the opened tags. After that, we wrap a <code class=\"lang-markup\">span<\/code> with the class <code class=\"lang-markup\">screen-reader-text<\/code> around the title. The <code class=\"lang-php\">else<\/code> code is the default behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Plugin<\/h2>\n\n\n\n<p>I created a small plugin that implements the feature as a widget. To use it, do the following after activation the plugin:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a menu with social media links<\/li><li>Choose \u00bbSVG Social Menu\u00ab as menu position<\/li><li>Drag the widget into a widget area<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>For my latest WordPress theme, I modified the output of a standard WordPress menu so that at the frontend URLs to social media channels are displayed as SVG icons. In this post, I show you my solution.<\/p>\n","protected":false},"author":1,"featured_media":3357,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development"],"wp-worthy-pixel":{"ignored":false,"public":"a95ad5b15e984c9dac3b0e037d4eefc8","server":"vg01.met.vgwort.de","url":"https:\/\/vg01.met.vgwort.de\/na\/a95ad5b15e984c9dac3b0e037d4eefc8"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3355"}],"version-history":[{"count":4,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3355\/revisions"}],"predecessor-version":[{"id":5943,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3355\/revisions\/5943"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media\/3357"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}