If you’re used to creating your own web icons, you must be familiar with Icomoon. For a client project already using FontAwesome, I needed to add 2 customs icons from using the Icomoon service.
In this blog post, I’m using Icomoon, but you should be able to reproduce the same behaviour with another icon provider, maybe with some adaptation.
FontAwesome icons were used on menu items thanks to Advanced Custom Fields and Advanced Custom Fields: Font Awesome Field. The goal is to add to the list of FontAwesome icons, my 2 or 3 customs icomoon icons.
Show Icomoon icons in the ACF FontAwesome field
Advanced Custom Fields: Font Awesome Field that I will name ACFFA from now offers a filter to modify the list of icons:
ACFFA_get_icons
add_filter( 'ACFFA_get_icons', 'cot_test' ); function cot_test( $icons ) { $icomoon = array( 'cierge' => array( 'name' => 'cierge', 'hex' => '\e90e', 'unicode' => '' ), 'calice' => array( 'name' => 'calice', 'hex' => '\e90f', 'unicode' => '' ), ); foreach ( $icomoon as $ico ) { $icons['list']['far'][ 'far ' . $ico['name'] ] = '<i class="icon-' . $ico['name'] . '"></i> ' . ucfirst( $ico['name'] ); $icons['details']['far']['far ' . $ico['name']]['hex'] = $ico['hex']; $icons['details']['far']['far ' . $ico['name']]['unicode'] = $ico['unicode']; } return $icons; }
Here we filter the existing list of FA icons to add our custom icons. Unfortunately, since the Matt Keys extension is developed for FA, only the FontAwesome categories are available, namely Brand (fab), Regular (far), Style (fas). I decided to add my icons in the Regular or Far categories. This is purely arbitrary.
DéLet’s break down the code above
array( 'cierge' => array( 'name' => 'cierge', 'hex' => '\e90e', 'unicode' => '' ),
Here I declare the Icomoon icons I’m going to want to use:
- name: the name of the icon in the list
- hex: the hexadecimal code provided by Icomoon
- unicode: the unicode of the Icomoon icon
Then, with a foreach loop, I’m going to feed the $icons
array containing the FontAwesome icons
Display icons in the back office and front office
To display the icons in back-office, the Icomoon CSS sheet with all the icons will have to be loaded on the back-end side.
For that, we’re going to add this code:
add_action( 'admin_print_styles', 'cot_load_icomoon_css' );
If you are used to developing your themes you know the WordPress action hook wp_enqueue_scripts
that allows you to load stylesheets and scripts in the frontend. There is also an equivalent for the back office called admin_print_styles
.
add_action( 'admin_print_styles', 'cot_load_icomoon_css' ); add_action( 'wp_enqueue_scripts', 'cot_load_icomoon_css', 9, 1 ); function cot_load_icomoon_css(){ wp_enqueue_style( 'font-icomoon', get_template_directory_uri() . '/css/iconmoon.css'); }
So here I load the CSS sheet in front and back office.
I put a priority 9 at the front loading in order to pass just before the loading of the theme stylesheet and to be able to use them afterwards in my theme. By default an add_action()
has a priority of 10.
Display the icons in front-office
In front-office, the ACF FontAwesome extension returns HTML markup formatted for FA (normal!). In some cases, we want to load another font (the one generated by Icomoon) in our custom icon.
Icomoon provided me with 4 font files (.eot, .svg, .ttf and .woff) that I placed in my projects. The icomoon.css file does a @font-face
so that the fonts are loaded
The problem lies in the fact that the Matt Keys extension generates an HTML FA markup and it will be necessary to “trick” to hijack the operation only for Icomoon icons. And yes, we want to be able to continue to use FontAwesome normally.
<i class="far cierge" aria-hidden="true"></i>
We find the far class of FA which will load the FontAwesome font then the cierge class generated from the name given in the list of extensions to insert.
To make the site load my font instead of FontAwesome’s, I created a CSS sheet modifying the style of these classes:
.far.cierge:before { font-family: "my-icomoon-font"; content: "\e90e"; }
For each custom icon I ask WordPress to load my-icomoon-font and use the content with the hex code corresponding to my icon.
Bonjour !
Votre tuto est super et c’est exactement ce dont j’ai besoin mais je ne sais pas ou modifier les bouts de code dont vous faîtes référence.
Dois-je créer un nouveau fichier php dans mon thème enfant ?
Dois-je éditer l’un des fichiers php de l’extension ACFFA ?
Merci pour votre réponse !
Bonjour,
Merci pour l’intérêt porté a mon article.
Par défaut, on ne modifie jamais un fichier d’une extension tierce. Lors d’une mise à jour de celle ci, votre modification serait tout bonnement écrasée.
C’est pourquoi j’utilise le filtre ACFFA_get_icons proposé par ACFFA pour modifier depuis une autre extension ou depuis votre thème enfant le comportement de cette extension.
http://www.guillaumevoisin.fr/tips-tricks/bien-comprendre-et-utiliser-les-hooks-wordpress
Bonjour Sébastien
Merci pour ce tutoriel.
J’essaie de comprendre comment votre install WP connait l’origine de vos icônes.
Je peux créer un set d’icon Icomoon, mais ensuite comment l’installer ? et ou ?
Par exemple, ou se trouve votre icône cierge ?
Bonjour Quentin,
Icomoon fourni un css pour charger les icones
https://codepen.io/sebastienserre/pen/OJmJaXz
Bonjour, très bon tuto !
Je l’ai suivi à la lettre et je bloc à la toute dernière partie, mon icon s’affiche en carré. Je pense que cela vient de cette ligne CSS : font-family: “ma-police-icomoon”;
Je ne vois pas par quoi remplacer “ma-police-icomoon” et ou récupérer cette info ?
Merci par avance !
Icomoon vous a fourni un fichier CSS “icomoon.css”. À l’intérieur il a donné un nom à votre police.
Il y a un premier bloc du type:
@font-face {
font-family: 'ma-police-icomoon';
src: url('../fonts/ma-police-icomoon.eot?ywth1d');
src: url('../fonts/ma-police-icomoon.eot?ywth1d#iefix') format('embedded-opentype'), url('../fonts/ma-police-icomoon.ttf?ywth1d') format('truetype'), url('../fonts/ma-police-icomoon.woff?ywth1d') format('woff'), url('../fonts/ma-police-icomoon.svg?ywth1d#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}
ou `ma-police-icomoon` est remplacé par le nom de votre police.