Blog

Adding A Custom Icon To The Social Links Block Should Not Be This Hard

I recently came across the need to add a custom icon to the Social Links block in WordPress. The icon I needed to add was for a platform called PlayHQ, a sports league management platform for many major sports in Australia. While it’s not exactly a social media platform, it’s pretty important to have quick and easy links to the platform as it’s where all fixtures, ladders and stats are kept. This also felt like a good opportunity to sink my teeth into some native block editor code, having given up trying to keep up with development years ago because of how unstable it was initially, and having almost exclusively relied on Advanced Custom Fields for custom blocks ever since.

I assumed it would be as simple as filtering an array of icons. How very wrong I was.

My first step was to look at the Gutenberg repository on GitHub. I’m not sure how much of what’s been merged into core is still available as uncompiled files, nor where I would find them, so searching the plugin meant it would easier to find what I was looking for. It didn’t take me long to find where all the blocks were stored, under /packages/block-library/src. Searching for the social links block, I found there were two similarly named blocks, social-link and social-links. This wasn’t a huge surprise, as the social links block acts as a wrapper block to house the icons, each icon being a block itself. As I’m just trying to add a custom icon I figured I probably just needed to look at the social-link block.

Now there are dozens of different services here and only one social link block. This is pretty smart, as for the most part the block will be the same, just the icon changing. In the variations.js file I could see a big array of all the different services. Each service being an object with some important information like the name of the icon, and the icon itself in SVG format stored as a variable. “This is easy”, I started to think to myself, “I’ve worked with block variations before”.

Except that wasn’t quite true. Following a little more research, it turns out this was the first time I’d encountered block variations. What I was thinking of was block styles, while very similar in theory they are very different in implementation. Adding custom block styles are easy by comparison, you can create a style with just a couple of lines of PHP. On the other hand, you can’t register block variations with PHP. This excellent video from Nick Diego at WP Engine does a really good job of highlighting the difference.

So now it was off to the documentation to see how to register a block variation. And to my pleasant surprise, the official documentation was quite good in this regard. Following the example code, I was able to reproduce creating a block variation in no time with just a few lines of code. Much credit must go to the documentation team as documentation around developing for the block editor has come a long way in recent times.

wp.domReady( function() {
wp.blocks.registerBlockVariation(
'core/social-link',
{
name: 'playhq',
attributes: { service: 'playhq' },
title: 'PlayHQ',
}
);
});

There was just one small problem, the icon. In all the core services, the icon is stored as a variable. Storing the icon as an SVG in a string isn’t hard, except that’s not what’s needed here. Believe me, I tried it. Using some form of ES6 trickery, it appears as though it’s storing it as some form of literal SVG object. I don’t know for certain, but that’s just my educated guess. The world of modern Javascript is still largely a mystery to me at this stage. This is where the real fun would begin.

Other than having to spend a bit of time learning how block variations work, everything made sense up until this point. However it was now clear I needed to add a new build process specifically for this block, just so that it could have an icon. Now I felt it was starting to get into the realm of overkill. This was also what had turned me away from developing for the block editor when it was first introduced. Because of my unfamiliararity with ES6, I was relying on other peoples build scripts, usually Zac Gordon’s from his WordPress Block Development course. Unfortunately, because of how incredibly unstable the environment was, the build script would stop working every time there was an update, and because I was relying on someone else’s build script, I had to wait for it to be updated accordingly to start developing again, and after being stuck in that cycle for a few weeks I walked away and never looked back. Thankfully, very soon after Block Lab (now Genesis Custom Blocks) and Advanced Custom Fields announced PHP based block creation tools and I and many others breathed a large sigh of relief. So now at this point I’m quite apprehensive about having to rely on someone else’s build script that will likely break with each WordPress update. I was almost prepared to give up at this stage but thankfully I didn’t.

I soon discovered that WordPress now has a Node package called @wordpress/scripts. A reusable collection of scripts provided by WordPress itself that includes the relevant build scripts that I need, yes please. At this stage I was still writing my custom code for this icon in the theme of the site I was working on, which was already using a custom Grunt script I had written to compile scripts and styles. My directory structure looks a bit like this:

  • assets
    • js
      • src
        • file1.js
        • file2.js
      • scripts.min.js
    • css
      • src
        • _file1.scss
        • _file2.scss
      • styles.min.css

If you’ve worked with the WordPress build scripts before, you will know that this structure is quite different to what the build script wants. So the first time I tried to run the build process for this specific script and saving the results alongside my other scripts, it wiped all my other scripts. If I wasn’t using Git for version control management, I would have lost all that previous work without warning. I tried changing the destination, and this time it wiped the entire theme. This is exactly the kind of terrible behaviour that deters people from using build scripts and the command line in general. Unfortunately most of the documentation around updating the destinations of these files is written for people who are creating custom blocks. Obviously, we aren’t creating a custom block here and as such there is no block.json file to edit. I eventually found a helpful article that did help me improve things with a Webpack configuration file, but it still wasn’t quite flexible enough to get the files in the right places. It had turned out that setting up a build script was just as painful as I’d expected, although not quite for the reasons I had anticipated. I was almost ready to give up, but I decided to move future development of this project into a plugin and give it one last shot. And finally, after being careful to arrange my files in a way the build script expected demanded, it worked!

import { Path, SVG } from '@wordpress/primitives';
export const PlayHqIcon = () => (
<SVG width="24" height="24" viewBox="0 0 24 24" version="1.1">
<Path fill="#4897D2" d="M23.3,5.4l-0.1,0.8C16.7,2.5,8.8,2.2,2,5.4v0c2.9-2.1,6.2-3.5,9.7-4.1c0.3-0.1,0.6-0.1,0.9-0.1 c3.6-0.5,7.3-0.1,10.8,1.1C23.4,3.3,23.4,4.3,23.3,5.4L23.3,5.4z"/>
<Path fill="#9057A3" d="M2,5.4C2,5.4,2,5.4,2,5.4C2,5.5,2,5.4,2,5.4L2,5.4z"/>
<Path fill="#9057A3" d="M12.6,1.1c-0.3,0-0.6,0.1-0.9,0.1C8.2,1.9,4.9,3.3,2,5.4C1.9,4.3,1.9,3.3,2,2.2C5.4,1,9,0.7,12.6,1.1z"/>
<Path d="M11.9,3.2C8.5,3.2,5.1,4,2,5.4c0,0-0.1,0-0.1,0C1.5,5.7,1,5.9,0.6,6.2c0.4,3.1,1.4,6.1,3,8.8l1-8.2 c0,0,0-0.1,0.1-0.1s0.1-0.1,0.1-0.1h2c0.1,0,0.2,0.1,0.2,0.2l-0.5,3.7c0,0,0,0.1,0.1,0.1h2.6c0,0,0,0,0.1,0c0,0,0,0,0-0.1l0.5-3.7 c0,0,0-0.1,0.1-0.1c0,0,0.1-0.1,0.1-0.1h2c0,0,0.1,0,0.1,0.1c0,0,0,0.1,0,0.1l-1.2,9.6c0,0,0,0.1-0.1,0.1c0,0-0.1,0.1-0.1,0.1h-2 c0,0-0.1,0-0.1-0.1c0,0,0-0.1,0-0.1l0.5-3.8c0,0,0-0.1-0.1-0.1H6.3c0,0-0.1,0-0.1,0.1l-0.6,5.1c1.8,2.2,3.9,4,6.3,5.4 c2.6-1.5,5-3.6,6.8-6l-1-1c-0.1,0-0.1,0.1-0.2,0.1c-0.6,0.3-1.3,0.5-2,0.5c-0.9,0-1.8-0.3-2.4-0.9c-0.6-0.6-0.9-1.5-0.9-2.3 c0-0.2,0-0.4,0-0.6l0.4-2.8c0.1-0.7,0.3-1.3,0.7-1.9c0.4-0.6,0.9-1,1.5-1.3c0.6-0.3,1.3-0.5,2-0.5c0.6,0,1.2,0.1,1.8,0.4 c0.5,0.2,0.9,0.6,1.2,1.1C20,8.4,20.1,9,20.1,9.6c0,0.2,0,0.4,0,0.6L19.7,13c0,0.4-0.1,0.7-0.3,1.1l0.7,0.7c0,0,0,0,0.1,0.1 c1.6-2.6,2.6-5.6,3-8.6C19.8,4.2,15.9,3.2,11.9,3.2L11.9,3.2z"/>
<Path d="M15.3,8.9c-0.3,0.3-0.5,0.7-0.6,1.2l-0.4,3c0,0.1,0,0.2,0,0.3c0,0.4,0.1,0.7,0.3,1c0.3,0.2,0.6,0.4,0.9,0.4 c0.4,0,0.8-0.2,1.1-0.5c0.3-0.3,0.5-0.7,0.6-1.2l0.4-3c0-0.1,0-0.2,0-0.3c0-0.4-0.1-0.7-0.3-1c-0.3-0.2-0.6-0.4-1-0.4 C16,8.5,15.6,8.6,15.3,8.9L15.3,8.9z"/>
</SVG>
);
wp.domReady( function() {
wp.blocks.registerBlockVariation(
'core/social-link',
{
name: 'playhq',
attributes: { service: 'playhq' },
title: 'PlayHQ',
icon: PlayHqIcon,
}
);
});

Now if you look carefully, you can see the icon working correctly in the block inserter. However, it wasn’t displaying in the block itself, we still had the default link icon. So it was back to the Gutenberg repo to figure out what was going on.

Looking at the social-list.js file, we can see it has two handy functions, getIconBySite() and getNameBySite(). Both of these functions search the variations variable we saw in the varations.js file that had all the data about each icon, and return either the name or the icon based on which variation is being used. That’s all well and good, except that variable doesn’t include our new custom icon because it’s searching the original variable, not the current list of registered variations. That code is being compiled and run before we even run our code, so there’s seemingly no way for us to inject our new icon into the original list. Yet again, I had hit a roadblock and felt like giving up.

Then I remembered, the only person who responded to my initial tweet asking if anyone had any idea how to do this had suggested looking into the blocks.registerBlockType filter. I knew of the filter but I’m not sure if I would have thought to look into it more if it wasn’t for Ross’ suggestion.

So after consulting the documentation for the blocks.registerBlockType filter, it wasn’t immediately clear whether this was possible yet. The filter simply allows a variable called settings to be changed, which has a ton of various different settings included in it. However, when I went back to the Gutenberg repository, I noticed in the index.js file where the block is registered, there is a variable called settings that is being passed to the block as it’s registered. And before it’s registered, there is an object added inside that settings object called variations, which is being imported from the variations.js file. A console.log confirmed my suspicions, this was it! I had found a way to filter the settings to include our custom icon in the variations variable so the icon could load properly. So I added a wp.hooks.addFilter call inside my wp.domReady function and hit refresh.

And nothing happened.

import { Path, SVG } from '@wordpress/primitives';
export const PlayHqIcon = () => (
<SVG width="24" height="24" viewBox="0 0 24 24" version="1.1">
<Path fill="#4897D2" d="M23.3,5.4l-0.1,0.8C16.7,2.5,8.8,2.2,2,5.4v0c2.9-2.1,6.2-3.5,9.7-4.1c0.3-0.1,0.6-0.1,0.9-0.1 c3.6-0.5,7.3-0.1,10.8,1.1C23.4,3.3,23.4,4.3,23.3,5.4L23.3,5.4z"/>
<Path fill="#9057A3" d="M2,5.4C2,5.4,2,5.4,2,5.4C2,5.5,2,5.4,2,5.4L2,5.4z"/>
<Path fill="#9057A3" d="M12.6,1.1c-0.3,0-0.6,0.1-0.9,0.1C8.2,1.9,4.9,3.3,2,5.4C1.9,4.3,1.9,3.3,2,2.2C5.4,1,9,0.7,12.6,1.1z"/>
<Path d="M11.9,3.2C8.5,3.2,5.1,4,2,5.4c0,0-0.1,0-0.1,0C1.5,5.7,1,5.9,0.6,6.2c0.4,3.1,1.4,6.1,3,8.8l1-8.2 c0,0,0-0.1,0.1-0.1s0.1-0.1,0.1-0.1h2c0.1,0,0.2,0.1,0.2,0.2l-0.5,3.7c0,0,0,0.1,0.1,0.1h2.6c0,0,0,0,0.1,0c0,0,0,0,0-0.1l0.5-3.7 c0,0,0-0.1,0.1-0.1c0,0,0.1-0.1,0.1-0.1h2c0,0,0.1,0,0.1,0.1c0,0,0,0.1,0,0.1l-1.2,9.6c0,0,0,0.1-0.1,0.1c0,0-0.1,0.1-0.1,0.1h-2 c0,0-0.1,0-0.1-0.1c0,0,0-0.1,0-0.1l0.5-3.8c0,0,0-0.1-0.1-0.1H6.3c0,0-0.1,0-0.1,0.1l-0.6,5.1c1.8,2.2,3.9,4,6.3,5.4 c2.6-1.5,5-3.6,6.8-6l-1-1c-0.1,0-0.1,0.1-0.2,0.1c-0.6,0.3-1.3,0.5-2,0.5c-0.9,0-1.8-0.3-2.4-0.9c-0.6-0.6-0.9-1.5-0.9-2.3 c0-0.2,0-0.4,0-0.6l0.4-2.8c0.1-0.7,0.3-1.3,0.7-1.9c0.4-0.6,0.9-1,1.5-1.3c0.6-0.3,1.3-0.5,2-0.5c0.6,0,1.2,0.1,1.8,0.4 c0.5,0.2,0.9,0.6,1.2,1.1C20,8.4,20.1,9,20.1,9.6c0,0.2,0,0.4,0,0.6L19.7,13c0,0.4-0.1,0.7-0.3,1.1l0.7,0.7c0,0,0,0,0.1,0.1 c1.6-2.6,2.6-5.6,3-8.6C19.8,4.2,15.9,3.2,11.9,3.2L11.9,3.2z"/>
<Path d="M15.3,8.9c-0.3,0.3-0.5,0.7-0.6,1.2l-0.4,3c0,0.1,0,0.2,0,0.3c0,0.4,0.1,0.7,0.3,1c0.3,0.2,0.6,0.4,0.9,0.4 c0.4,0,0.8-0.2,1.1-0.5c0.3-0.3,0.5-0.7,0.6-1.2l0.4-3c0-0.1,0-0.2,0-0.3c0-0.4-0.1-0.7-0.3-1c-0.3-0.2-0.6-0.4-1-0.4 C16,8.5,15.6,8.6,15.3,8.9L15.3,8.9z"/>
</SVG>
);
const PlayHqSocialLink = {
name: 'playhq',
attributes: { service: 'playhq' },
title: 'PlayHQ',
icon: PlayHqIcon,
};
wp.domReady( function() {
wp.hooks.addFilter(
'blocks.registerBlockType',
'playhq-social-icon/social-link-block',
function( settings, name ) {
if ( 'core/social-link' === name ) {
if ( settings.variations.length ) {
var variation = PlayHqSocialLink;
settings.variations.push( variation );
}
}
return settings;
}
);
wp.blocks.registerBlockVariation( 'core/social-link', PlayHqSocialLink );
});

It’s worth noting here that we’re starting to see the first signs of having to repeat ourselves to try and get this thing working. You’ll notice I’m now storing the PlayHQ icon object in a variable, and passing that variable to registerBlockVariation as well as having to add it to the settings.variations array. Variables are designed to be reused, but I don’t think it’s ideal having to pass the same variation data multiple times here. None of this would have even been neccesary if the getIconBySite() and getNameBySite() functions were checking against the registered variations for the block and not just the originally defined variations. I’m hoping that’s due to a technical limitation and not just laziness.

Adding scripts inside a document ready function is a very standard best practice. As someone who’s written most of their Javascript related to the jQuery library, pretty much every line of Javascript I’ve written has been inside a document ready function. You generally don’t want to be executing scripts before the document has finished loading, otherwise things will break. However in this case, for some reason, the filter never runs. When I moved the filter from inside the document ready function, it finally worked. At last, the icon now displayed in the block itself.

Unfortunately, you might notice that there are still issues here. The icon is showing up correctly in the block itself, but not in the contextual toolbar or the block settings panel on the right, and the name of the block is still just “Social Icon”. For crying out loud. To figure out why, we need to go back to the Gutenberg repository and have another look at the variations.js file. At the bottom of that file we can see a little loop that’s setting an isActive property for each variation. I guessed that the way it picks up which icon and name to display is based on this isActive property, so I copied the code over from that variations.js file and tried yet again.

import { Path, SVG } from '@wordpress/primitives';
export const PlayHqIcon = () => (
<SVG width="24" height="24" viewBox="0 0 24 24" version="1.1">
<Path fill="#4897D2" d="M23.3,5.4l-0.1,0.8C16.7,2.5,8.8,2.2,2,5.4v0c2.9-2.1,6.2-3.5,9.7-4.1c0.3-0.1,0.6-0.1,0.9-0.1 c3.6-0.5,7.3-0.1,10.8,1.1C23.4,3.3,23.4,4.3,23.3,5.4L23.3,5.4z"/>
<Path fill="#9057A3" d="M2,5.4C2,5.4,2,5.4,2,5.4C2,5.5,2,5.4,2,5.4L2,5.4z"/>
<Path fill="#9057A3" d="M12.6,1.1c-0.3,0-0.6,0.1-0.9,0.1C8.2,1.9,4.9,3.3,2,5.4C1.9,4.3,1.9,3.3,2,2.2C5.4,1,9,0.7,12.6,1.1z"/>
<Path d="M11.9,3.2C8.5,3.2,5.1,4,2,5.4c0,0-0.1,0-0.1,0C1.5,5.7,1,5.9,0.6,6.2c0.4,3.1,1.4,6.1,3,8.8l1-8.2 c0,0,0-0.1,0.1-0.1s0.1-0.1,0.1-0.1h2c0.1,0,0.2,0.1,0.2,0.2l-0.5,3.7c0,0,0,0.1,0.1,0.1h2.6c0,0,0,0,0.1,0c0,0,0,0,0-0.1l0.5-3.7 c0,0,0-0.1,0.1-0.1c0,0,0.1-0.1,0.1-0.1h2c0,0,0.1,0,0.1,0.1c0,0,0,0.1,0,0.1l-1.2,9.6c0,0,0,0.1-0.1,0.1c0,0-0.1,0.1-0.1,0.1h-2 c0,0-0.1,0-0.1-0.1c0,0,0-0.1,0-0.1l0.5-3.8c0,0,0-0.1-0.1-0.1H6.3c0,0-0.1,0-0.1,0.1l-0.6,5.1c1.8,2.2,3.9,4,6.3,5.4 c2.6-1.5,5-3.6,6.8-6l-1-1c-0.1,0-0.1,0.1-0.2,0.1c-0.6,0.3-1.3,0.5-2,0.5c-0.9,0-1.8-0.3-2.4-0.9c-0.6-0.6-0.9-1.5-0.9-2.3 c0-0.2,0-0.4,0-0.6l0.4-2.8c0.1-0.7,0.3-1.3,0.7-1.9c0.4-0.6,0.9-1,1.5-1.3c0.6-0.3,1.3-0.5,2-0.5c0.6,0,1.2,0.1,1.8,0.4 c0.5,0.2,0.9,0.6,1.2,1.1C20,8.4,20.1,9,20.1,9.6c0,0.2,0,0.4,0,0.6L19.7,13c0,0.4-0.1,0.7-0.3,1.1l0.7,0.7c0,0,0,0,0.1,0.1 c1.6-2.6,2.6-5.6,3-8.6C19.8,4.2,15.9,3.2,11.9,3.2L11.9,3.2z"/>
<Path d="M15.3,8.9c-0.3,0.3-0.5,0.7-0.6,1.2l-0.4,3c0,0.1,0,0.2,0,0.3c0,0.4,0.1,0.7,0.3,1c0.3,0.2,0.6,0.4,0.9,0.4 c0.4,0,0.8-0.2,1.1-0.5c0.3-0.3,0.5-0.7,0.6-1.2l0.4-3c0-0.1,0-0.2,0-0.3c0-0.4-0.1-0.7-0.3-1c-0.3-0.2-0.6-0.4-1-0.4 C16,8.5,15.6,8.6,15.3,8.9L15.3,8.9z"/>
</SVG>
);
const PlayHqSocialLink = {
name: 'playhq',
attributes: { service: 'playhq' },
title: 'PlayHQ',
icon: PlayHqIcon,
};
wp.hooks.addFilter(
'blocks.registerBlockType',
'playhq-social-icon/social-link-block',
function( settings, name ) {
if ( 'core/social-link' === name ) {
if ( settings.variations.length ) {
var variation = PlayHqSocialLink;
if ( ! variation.isActive ) {
variation.isActive = ( blockAttributes, variationAttributes ) => blockAttributes.service === variationAttributes.service;
}
settings.variations.push( variation );
}
}
return settings;
}
);
wp.domReady( function() {
wp.blocks.registerBlockVariation( 'core/social-link', PlayHqSocialLink );
});
view raw working-js.js hosted with ❤ by GitHub

At long last, it was finally finished. The variation was added, the icon and name showed up everywhere they were supposed to, and the build process worked seamlessly. Now to check the front end…

And now is where I started getting angry. I’d put in so much work to get to this point and on the front end, the thing that literally everyone else but me will see, I have nothing to show for it.

Now I’m effectively back to square one. Having looked at the content the block creates, it turns out it’s a dynamic block, so the icon doesn’t actually save any markup, including the icon. That means that the actual HTML is being defined somewhere in PHP. I feel much more comfortable working with PHP and having sorted the editor, I felt like I was on the home stretch at least.

Now back to the GitHub repository yet again, opening up the index.php file gave me what I needed. I followed the rabbit trail until I found the block_core_social_link_services() function. This looks awfully familiar. In fact, it’s effectively just the variations Javascript variable, but written for PHP. Now in true WordPress fashion, there should be a filter at the end of the function that allows me to just inject my new icon.

Except there isn’t. There is no apply_filters function call in the entire file. I checked the WordPress core code, just in case things were different between the Gutenberg plugin and core and found the same thing. No filter. Now there’s no technical limitation here, this is laziness to not bother to include a filter. Or, dare I say it, perhaps arrogance that the list was exhaustive and would never needed to be expanded upon by a third party. I sure hope not.

Again we’ll notice that the code is repeating itself again, and this time we can’t rely on variables. The SVG markup is being defined in both PHP and Javascript to be used for this block. I’m hoping there’s a good reason as to why the icons weren’t just saved as SVG files and imported as required instead of having to write the same code multiple times. I can forsee some new contributor who thinks that updating an icon following a redesign is an easy patch to make, only to be completely demoralised when they realise that the code is not DRY. I’m hoping there’s a good reason for this, because I’m not entirely sure why the developers have ignored such fundamental development practices.

Now that there’s no filter for the list of services and their icons, we’re going to have to rely on filtering the full markup of the block. Thankfully, as it’s a block variation, we can be fairly confident of the structure of the block. For this, we can use the render_block filter. In fact, Gravity Forms has some good examples of how to manipulate HTML markup with PHP. So I tried to use the DOMDocument class to replace the markup of the SVG with our new PlayHQ icon.

Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity

Oh come on. It’s not even WordPress letting me down this time. Apparently DOMDocument still can’t handle HTML5 tags, which is frankly embarassing.

So now that DOMDocument is off the table, I had to figure out a new approach to replace the SVG markup. I’ve used the render_block filter a few times in the past, while not ideal for this situation, I know at the very least I can edit the block markup. Thankfully, the block will only display one icon at a time, so will only have one SVG element. Considering that, I decided to try exploding the markup. Get the markup before the SVG, and the markup after the SVG, and inject the new SVG icon in the middle. As disgusting as it felt to write, the code worked. After dozens of hours across several days, plenty of swearing, screaming, hitting the desk and overthinking, we finally have a new social link icon working.

<?php
add_filter(
'render_block',
function( $block_content, $block ) {
if ( 'core/social-link' === $block['blockName'] && 'playhq' === $block['attrs']['service'] ) {
$icon = '<svg width="24" height="24" viewBox="0 0 24 24" version="1.1">
<path d="M20.9,4.16l-.08,.71C14.83,1.48,7.57,1.22,1.35,4.17v-.03C3.98,2.19,7.03,.88,10.25,.31c.28-.05,.54-.08,.81-.11,3.34-.46,6.74-.12,9.92,1,.04,.99,.01,1.97-.08,2.95Z" fill="#4897d2"/>
<path d="M1.35,4.14s-.06,.04-.09,.07c.03-.01,.06-.03,.09-.04v-.03Z" fill="#9057a3"/>
<path d="M11.06,.2c-.27,.04-.54,.07-.81,.11C7.03,.88,3.98,2.19,1.35,4.14c-.09-.97-.12-1.95-.08-2.92C4.41,.11,7.76-.24,11.06,.2Z" fill="#9057a3"/>
<path class="cls-3" d="M10.41,2.13c-3.13,0-6.23,.7-9.06,2.04-.03,.01-.06,.02-.09,.04-.43,.21-.85,.42-1.26,.66,.37,2.85,1.32,5.6,2.79,8.08l.93-7.59s.02-.08,.05-.11c.03-.03,.08-.05,.12-.05h1.8c.1,0,.15,.05,.15,.16l-.42,3.4s.02,.07,.06,.07h2.4s.03,0,.05-.02c.01-.01,.02-.03,.02-.05l.42-3.4s.03-.08,.06-.11c.03-.03,.07-.05,.11-.05h1.82s.08,.02,.1,.05c.03,.03,.04,.07,.03,.11l-1.09,8.9s-.03,.08-.06,.11c-.03,.03-.07,.05-.11,.05h-1.82s-.08-.02-.11-.05c-.03-.03-.04-.07-.03-.11l.42-3.5s0-.07-.05-.07h-2.38s-.06,.02-.08,.07l-.57,4.75c1.62,1.99,3.59,3.67,5.81,4.96,2.43-1.4,4.56-3.28,6.25-5.52l-.93-.94c-.07,.04-.12,.09-.19,.13-.58,.29-1.23,.44-1.88,.43-.83,.04-1.63-.24-2.25-.79-.57-.56-.87-1.34-.83-2.13,0-.17,0-.34,.03-.51l.33-2.63c.07-.64,.29-1.24,.65-1.77,.35-.51,.82-.92,1.37-1.2,.58-.29,1.22-.44,1.86-.43,.57-.01,1.13,.11,1.65,.36,.45,.22,.82,.57,1.07,1.01,.26,.46,.39,.99,.37,1.52,0,.17,0,.34-.03,.51l-.32,2.63c-.04,.34-.12,.67-.23,.99l.66,.66s.04,.04,.08,.07c1.45-2.44,2.39-5.16,2.75-7.98-3.18-1.8-6.77-2.75-10.42-2.74Z"/>
<path class="cls-3" d="M13.55,7.38c-.29,.29-.47,.68-.51,1.09l-.33,2.73c0,.09-.01,.18-.01,.26-.02,.33,.09,.66,.32,.91,.23,.23,.54,.35,.86,.33,.39,0,.77-.14,1.05-.42,.29-.29,.47-.68,.51-1.09l.34-2.73c.01-.08,.01-.16,.01-.24,.02-.34-.1-.67-.32-.92-.23-.23-.55-.36-.88-.34-.39,0-.76,.14-1.03,.41Z"/>
</svg>';
$before = explode( '<svg', $block_content );
$after = explode( '</svg>', $before[1] );
$block_content = $before[0] . $icon . $after[1];
}
return $block_content;
},
10,
2
);

Well, except it’s not even done yet. There’s still styles to write for the different colour schemes. But at least I won’t have to jump through any hoops to write some CSS.

There is no good reason why it has to be this hard. While what I’m trying to achieve is absolutely niche, it’s quite sad to see such basic development principles like DRY code and allowing data to be filtered be completely abandoned. In an ideal world, I should just be able to add a filter that points to an SVG icon file.

That said, I’m far more optimistic about developing for the block editor than before I started this little experiment. I came into this prepared to be disappointed and almost expecting to be unable to complete it, but I did. The provision of the @wordpress/scripts package and much improved documentation makes me feel far more confident in starting to approach development for the block editor again.

But it seems we still have a long way to go.

Did you find this post useful?

YesNo