Add custom icons to your ACF with ACF projects: FontAwesome Field

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' => '&#xe90e;'
),

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

Exemple d'icône personalisé dans le champs ACF

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.

 

Install WordPress Coding Standards on PHPStorm easily

WordPress, as all development frameworks, has its own rules to follow so that each developer can easily read and understand a colleague’s code.

This is what we call “Coding Standards”. For WordPress, we have the WordPress Coding Standards better known as WPCS!

These developments rules can be read here: Discover the WordPress Coding StandardsOK, we’ve got a thousand pages of docs… but I’m not gonna read all those docs while developing… couldn’t you make it easier?

Well you’re lucky because we can integrate these rules directly into our IDEs or text editors! So your code will be automatically checked during your development.

I use PHPStorm from Jetbrains every day but you should know that these rules can be used on almost any code editor.

Where do I find the WPCS?

WPCS are available on Github. I will detail how I do it but there are several methods. Take your pick!

Séb method for installing WPCS

So it’s not really my method… in the sense that it’s not me who developed it… but it’s the one I’m using and that I’m going to detail!

Before installing WPCS, you need to install PHPCS… and yes, WordPress is developed in PHP and to make it simple, WPCS adds rules or modify a little the rules of the PHP Codings Standards.

Keep in mind that, as in all the WordPress ecosystem, the goal is to make development accessible and easy.

Install PHPCS

Personally, I have a partition on my Ubuntu Linux dedicated to my projects and next to the projects, I have a folder with the WPCS and one with the PHPCS.

PHPCS are also available on Github

Clone PHPCS Github repo

Go to the place where you want to download the rules then:

Plain Text

or

Plain Text

if you haven’t entered an SSH key in your GitHub account.

You should now have a folder named phpcs.

Clone WPCS Github repo

We’ll do almost the same command to clone the WPCS repository this time:

Plain Text

or

Plain Text
That’s great news! You have installed PHPCS and WPCS on your computer!

I reassure you, the hardest part is over…

Setting PHPStorm

Go to “File | Settings | Languages & Frameworks | PHP | Quality Tools”.

Programme d'ordinateur
PHPStorm settings

By clicking on the 3 points to the right of Configuration you can specify the path to the PHPCS. It will be necessary to navigate and select the file “phpcs” present in the directory “phpcs/bin” obtained thanks to the preceding cloning.

Capture d'écran

If everything is OK, at the bottom of the window will show the version of Code Sniffer installed.

Let’s move on to WPCS, because after all… they’re the reason we’re here!

The path to the settings is File | Settings | Editor | Inspections

Logiciel
I love arrows!

the goal here is to get to the “PHP Code Sniffer Validation” menu in order to check “Installed Standard Paths” and to search after clicking on the folder on the right for the directory to be cloned with WPCS.

If all goes well… by clicking on the 2 circular arrows, you should have the choice of several WPCS… personally, I choose WordPress-Extra. then we validate by clicking OK…

BRAVO! you are ready to develop for WordPress according to the rules developed by the community and used in the Core of your favorite CMS.

3 minutes to install WordPress!

We read everywhere that it takes 5 minutes to install WordPress… and it’s FALSE! We’ve been lied to! We can install it much faster than that…

The proof in video!

Those of you who have already installed a multisite know that after installing WordPress, you have to enter constants in the wp-config.php to activate this mode, go to the Back-Office, configure the network and return to the wp-config.php to paste a pad of constants as well as in the .htaccess … in short much more than with the script I have concocted!

How does it work ?

I’ve created a bash script that, when you run it, asks you a few questions.

Nothing nasty, it’s the information you would enter during the routine of installing a WordPress via your favorite browser.

Once the answers are recorded, the script launches the installation and the multisite is ready … in 3 minutes!

Of course this script will need wp-cli to work.

Command line interface for WordPress | WP-CLI

Command line interface for WordPress | WP-CLI

WP-CLI is the command-line interface for WordPress. You can update plugins, configure multisite installs and much more, without using a web browser.

https://wp-cli.org

I’d like to have the Seb Scripts!

Yeat get it on Github 😉

Geolocalize an address without Google Maps

We’ve long been used to using Google’s tools and services to do a lot of things… only good things come to an end and some of these services close or become paid for.

As part of the development of my OpenAgenda plugin for WordPress, I need to find the longitude and latitude of an event address.

OpenStreetMap to the rescue

Fortunately, there are many services that can replace those of Google. Among them OpenStreetMap (OSM) for everything that revolves around maps and geolocation.

Nominatim for the conversion of address to location data.

So I discovered the Nominatim service.

That’s all very nice, but I want code!

If like me, you need to retrieve the latitude and longitude of a geographic point in PHP… for WordPress, here’s what I do:

PHP

A little explanation:

Line 1: a function is declared in which I pass by setting an address (street postal code city).

Line 2: I encode this address so that it can be integrated into a URL.

Line 3: I check if a transition with the coordinates exists. Nominatim limits access to its IAP… the transient allows you to store the information locally and not to make a request at each display… especially since the geographical coordinates of a point should not change too often….

Line 14: I create the transient that will be valid for 90 days. Basically, your WordPress will ask for geolocation only once every 90 days for the same address.

Between 2: if the address has never been geocoded then I request the Nominatim API.

Any ideas or improvements to make? Leave me a comment!

Develop your WordPress Project using Lando

I already told you about Lando a while ago… it was more of a cry from the heart to have found a simple solution to use Docker technology.

What’s Lando?

As I explained in my previous article, Lando is an overlay that makes it easy to use Docker Technology.

To put it simply, you have to see Docker as a modular tool where each brick is the component of your web server.
So you will need for example, to run a WordPress locally an Apache brick, a PHP brick and a Mariadb or Mysql brick.
Among these different bricks, there are different versions. For example PHP7.0 / PHP7.3 .

To develop soundly and avoid unpleasant surprises during production, it can be interesting to use during our developments the same software versions as those used by the server that will host the website in the end. In this way we reduce the risks of conflicts and errors.

Docker’s goal is to provide these different bricks and make them work together… except that…

Except that, as I said in my previous article, you have to set all this up… and have sysadmin knowledge that we don’t all have… fortunately Lando has arrived…

Install Lando

The hardest part of installing Lando is actually installing Docker which is quite simple if you are free like me, i.e. on a Linux distribution. It’s a bit more complicated if you’re trapped in a private system such as Windows or Mac OS. (For once we have something simpler under Ubuntu Linux… )

In short, Docker runs natively under Linux, you’ll have to add a virtual machine to install Docker on Windows or Mac OS.

Once Docker is installed, go to Lando’s website to install the overlay that will set all this to music.

Configuring Lando

I told you earlier that Lando was ignoring the complexity of Docker’s parameterization.

All the configuration of your development server will fit in a text file.

Let’s imagine that we want to create a WordPress site with the url https://mon-super-site.lndo.site.

Just create a folder on your machine where you have the write access and that you would name with the name you want: my-super-site.

In this folder we will add a .lando.yml file that will contain all the configuration of the server of our site:

Plain Text
.lando.yml

That’s it, that’s all! Lando is ready to go! And you’re ready to develop!

Let’s decorate the .lando.yml:

  • Line 1: A name for your development environment. This name will be used to build the url of your project.
  • Line 2: “Recipe” means recipe in English. Lando has developed preconfigurations according to what you will do with it. Here is a WordPress recipe, it will install wp-cli. There are some for Pantheon, Drupal and more => Lando Recipe
  • Line 3: a config block where you can specify your software versions
  • Line 7: a Service block where we will list the services we want to see working. here PhpMyadmin and Mailhog
  • Line 16: Composing is still practical when working in the PHP world.
  • Line 19: This block is optional if you are working on a simple domain. It can be very useful when working on a Multisite WordPress subdomain for example.
  • Line 22: maybe the nicest part… do some tasks just after importing a database… here we do via wp-cli a search & replace to adapt the urls and we disable the excellent WordPress WP-Rocket extension because the cache in dev… how to say 😉

This .lando.yml is quite simple and there is a multitude of other configurations and possibilities to best fit your needs.

Note that Lando is oriented for development and is strongly discouraged by its developers for a production

To run Lando for the first time, you have to do a `lando rebuild –y’.

At this point, it will create and download what it needs. This step can be long depending on your web connection but it will only be done once…

Then for the next launches a `lando start` in your terminal in your working directory will be enough.

Attention the WordPress recipe prepares lando technically to receive WordPress but does not install WordPress. You will perform this installation step as you are used to or by using my wp-cli method.

I use Local By Flywheel… it’s the same thing!

Hummm each solution has its advantages and disadvantages. When I decided to test and finally adopt Lando, Local By Flywheel was not available under Linux. This is no longer the case since the summer of 2019.

I (too) quickly tested Local By Flywheel and I couldn’t get a WP to work :-/ (note it too much).

The only thing Flywheel brings for my case is a GUI (Graphical User Interface) whereas Lando… it’s command-line… but sincerely to make lando start or `lando rebuild –y`? install an interface that will necessarily use machine resources… I didn’t see the point.

Publish your WordPress plugin thanks to Github Actions!

This article will surely speak to WordPress extension developers who publish to the WordPress repository… and those who don’t publish to it so they don’t have to use SVN.

Indeed SVN is in a way the ancestor of GIT and nowadays rare are the developers who have not switched to Git.

Github is a service hosting GIT repositories that devs like particularly and very much used in the Open-Source world.

A few weeks ago, GitHub released the Github actions.

OK, but what is Github actions?

Simply put, it’s a solution to automate tasks when an action occurs on a repository or a branch of a repository (pull / push…)

That’s where it gets interesting!

Previously, for my personal example, I would version my code on Github and then I would transfer my WordPress extension files by hand to the WordPress.org SVN.

It works… but it’s long and boring!

Thanks 10Up!

10Up is one of the big WordPress agencies in the world and they just published an action that allows you to publish on the WordPress repo when you push on a branch of a Git repo, a particular Tag.

This tag will be included in the WordPress repository in order to publish the new version of your work.

How do we do it?

I don’t know if it’s the way to do it the right way as they say… but it’s the way I use it… it will surely get better with time, experience, cough cough cough.

In my project, I create a .github folder, note the dot in front and then a workflows subfolder.

Create in this last folder a file named `main.yml`:

Commit, then push this file.

Github Settings

Go to your Github repository, in the “Actions” tab you should find the action created earlier.

Marque
Github Actions

We used a SVN_PASSWORD variable and a SVN_USERNAME corresponding to the SVN IDs needed to publish on the WordPress repo. We’ll need to pass them to Github so that it can connect securely.

Go to the “Settings” tab.

page Web
Github Actions Settings

Click on “Add a new secret”

Capture d'écran
Github Actions Secret

In the “Name” input, enter the 1st variable name “SVN_PASSWORD” and enter the password of your wp.org account in the “Value” textarea.

Repeat, adapting for the variable corresponding to your password.

Test!

If all went well, the next time you create and push a Git tag, your extension will automatically be published to the Official WordPress repository.