I’m really counting on your help in this one. I searched a lot and found no solution. I want to have a custom icon for my plugin in admin menu, and I want it to integrate well with the color scheme.
I looked here https://codex.wordpress.org/Function_Reference/add_menu_page
Under $icon_url
(WP 3.8+) If ‘data:image/svg+xml;base64…’, the specified SVG data
image is used as a CSS background
However, if I put a URL to an SVG icon there, all I get is an img with SVG in its src attribute, so it doesn’t integrate at all with the color scheme. It’s supposed to be a CSS background.
Also, I don’t understand this data:image/svg+xml;base64...
What does it mean exactly?
I tried embedding inline SVG in the $icon_url
but obviously, it won’t work, but I just had to try it.
Dashicons are not an option, there’s no icon there suitable for my project.
Step by Step example using FontAwesome:
Including color and custom post types ????
1 Pick an icon
2 Get the SVG
fa
prefix – in my case, that is “flask.svg”.Bring the SVG into WordPress
Inside your
functions.php
, where you register your custom post type, add the following snippet:Important notes:
base64_encode
is the copied raw string from Font Awesomes github page.fill="black"
attribute to the path/s elements – this allows the color to be change by WordPress.I got the solution by analyzing Woocommerce. If no url is supplied to the
add_menu_page
function, WordPress uses the default dashicon. So it’s just a matter of overriding the default style. Like add this to admin styles:I converted svg to font on Icomoon.
Adding custom SVG icons.
I wanted my own custom SVG icon in my wordpress plugin, so had a look at this: https://developer.wordpress.org/reference/functions/add_menu_page/ and it all seems easy enough on paper. The guide tells you how to prepare the
$icon_url
parameter:However, I wrestled with this a bit and wanted to add in some notes when using the $icon_url parameter. From my experience and from looking on forums, using your custom icon isn’t as easy as just creating your SVG and encoding it. Here’s the step by step process to add custom SVG icon to the admin menu:
…and that will give you a valid $icon_url string. Let me expand on those steps.
Step 1
You can create your SVG graphic in any way you want as far as I can see. I created mine in Adobe Illustrator, but anything like Inkscape, Corel draw, even hardcoding if you’re comfortable with that.
Step 2
The most painful part is cleaning up your SVG. However I think this is also one of the most important parts. After a lot of testing and experimenting I found that the cleaner the SVG is, the more likely it is to work. A lot of graphics programs add in metadata and other fluff to make the SVG more widely compatible. That’s great 99% of the time, but when using it for a wordpress menu icon, it often breaks, resulting in no icon being shown at all.
This is an example of an SVG exported normally from Adobe Illustrator:
Messy, very messy. Even when we add some line breaks and indent it, there’s a lot going on.
So what we want to do is clean this up a lot and get rid of unnecessary data. You may find that the above will load if you add it as an image source but it won’t color properly with the GUI and it will probably be the wrong size. So there’s a few things we need to do:
using the tag. For each path, work out which classes apply to the
path from the tag, and add the CSS from those classes into the path.
<g>
group elements and leave the main path elements at the root of the SVG.add fill=”black” to each path.
width=”20″ height=”20″ into the opening element
Once that’s done, my SVG file from above now looks like this:
Step 3
Now you want to base64 encode your SVG. The way I did it was to pass the file into PHP’s default encoding mechanism, and copy the path into my PHP as an absolute path. You don’t want PHP to read the SVG file, base64 encode it, and pass it to the menu everytime the user reloads the page, that’s just a waste of resources and time. Easy enough with PHP and shouldn’t be a problem if you’re a wordpress dev.
Step 4
Add ‘data:image/svg+xml;base64,’ in front of the base64 SVG so it looks like this:
and that there, sir, is a perfectly valid $icon_url string. I would hard code that string to a variable and simply add it into your plugin every time.
I hope that helps someone else, because that would have saved me several hours this morning!
After you have converted the
icondata
in base 64, correct way to put it is like this;The “,” is important
Just thought I should add the following:
To get the SVG to automatically re-colour to match the theme, it needs to have a fill attribute set. This is because it’s dynamically altered through script, and it otherwise won’t know what part to re-colour.
The
svg
file must have thefill
attribute set in it to work. Open the svg file in an editor like Atom, and make sure it looks like this:<path fill="black" d="....
You can put any kind of color you want in there, WordPress uses JS to automatically update the fill value based on the admin theme.
A third alternative to CSS or SVG import, is to convert the SVG image to Base64 and use the
$icon_data_uri = 'data:image/svg+xml;base64,' . $icon_base64;
Disclaimer: I didn’t make this solution, however I do want to share it.
Full credit should go to the editorial staff @ https://daext.com/.
They made a lovely guide for generating a svg and applying it to WordPress Admin Menu. Source
To convert SVG to Base64 visit: base64 and copy-paste the converted values into $icon_base64.
To ensure consistency, if links are broken, the main points are listed below:
Step 1) Create a new folder named “my_custom_plugin”, and place it in: “/wp-content/plugins/{my_custom_plugin}”
Step 2) Create a “.php” file, within “my_custom_plugin” folder and name it e.g. “my-plugin.php”
Step 3) Copy paste following code below in to the file:
Step 4) go to the dashbord of WordPress, and “activate” the plugin: “plugin”->”installed plugin”. Look for the Plugin Name: “Custom-Plugin“. Which where assigned at the top of the plugin file in “my-plugin.php“
This should give an idea on how to get started using plugins. However, please remember, that some features should only be initiated once to ensure a good user experience. Plugin Handbook
You have to paste a Base64 encoded image (data URI), into the
src
…More on Wikipedia.