The order of arguments in an if statement

Consider this dummy snippet:

if ( true === $skip || heavy_check() ){

The $skip is checked first. It is very quick as it is just a simple variable check. and since this is an ‘or’ statement. As soon as $skip is true it won’t bother with the heavy_check.

Something I always suspected but today I tested this and confirmed it.

Add custom post_meta to the WordPress rest API

So I needed some metadata to be in the rest API of a custom post_type.
The following adds the subtitle to the book post_type

add_action('rest_api_init', function () {
    register_rest_field('book', 'subtitle', [
        'get_callback'    => 'get_meta_data_for_rest',
        'update_callback' => null,
        'schema'          => null,


function get_meta_data_for_rest($post, $field_name, $request)
    // it's not always an object
    if (isset($post->id) && ! empty($post->id)) {
        $post_id = $post->id;
    } elseif (isset($post['id']) && ! empty($post['id'])) {
        $post_id = $post['id'];
    } else {
        return null;

    return get_post_meta($post_id, $field_name, true);

Delete all empty taxonomie terms in WordPress

The code below will find and delete all empty terms of every taxonomie, including the menu.
Think before you delete, you might want to keep some empty categories.

foreach ( get_taxonomies() as $tax_slug ) {//
    $terms = get_terms( $tax_slug, array( 'hide_empty' => false ) );

    /** @var WP_Term $wp_term */
    foreach ( $terms as $wp_term ) {
        if ( 0 == $wp_term->count ) {
            wp_delete_term( $wp_term->term_id, $wp_term->taxonomy );

Regex lookahead

Regex is one of the things that I’ve always regret I didn’t learn earlier. Yes it’s complicated, but without a doubt it is worth it. There is one syntax I didn’t know yet. The lookahead


Match everything until you find the letter a. The a itself is not included. Should be handy to parse url’s and such

reapplying a reverted commit

So I made a booboo and already pushed a merge commit.
The merged branch had a few commits that where not ready for develop.
After reverting the merge one commit needed to be reapplied.
But a merge wont work, because it already is merged (and reverted).
What you can do is a cherry pick.

How I created the problem

git merge branch
git push origin develop

Here there was no way back, accept rebasing but that’s also not very easy.

How I solved it.

git revert eaf8c471 -m2
git cherry-pick fa9a6b0

Cherry pick just straight up applies the changes made in the files.
Where a merge applies the git changes.

Keep in mind this will only help if you need a handfull of commits reapplied. Otherwise you will need to find an otherway.
Or cherry pick a lot…

Setup Letsencrypt SSL on raspberry pi

I love letsencrypt. It’s free SSL, it’s saver because of the auto-renewal and it’s so easy to setup. No more emailing around validating company name and whatnot.

If you’ve followed the previous steps you would not have installed git, check with git --version When it gives an error install git first with sudo apt-get install -y git

Now get the letsencrypt software and prepare the folder which letsencrypt will use.

sudo git clone /opt/letsencrypt
sudo mkdir /var/www/letsencrypt
sudo chown www-data:www-data /var/www/letsencrypt

Add the first part of the letsencrypt config to nginx sudo nano /etc/nginx/sites-enabled/
Before the final } add this:

location /.well-known/acme-challenge {
    root /var/www/letsencrypt;

And reload nginx sudo nginx -t && sudo service nginx reload

Getting the ssl certificate

So now we can generate and validate the ssl certificates. With the command below.
The email-address is used only for checks and warnings so I recommend an email which you do check. And of course change the domain. We will get a certificate for both www and non-www domain.

sudo /opt/letsencrypt/certbot-auto certonly -a webroot --webroot-path=/var/www/letsencrypt/ --rsa-key-size=4096 -m -d -d

When running this the first time It might take a long time, just be patient. When it is done we add the certificates to the nginx configuration: sudo nano /etc/nginx/sites-enabled/

listen 443 ssl default_server;

ssl_certificate /etc/letsencrypt/live/;
ssl_certificate_key /etc/letsencrypt/live/;

sudo service nginx reload

Test if ssl is working on both www and non-www. If it’s working then the next step is forcing ssl all the time.


server {
    listen 80;

And replace it with:

server {
    listen      80;
    rewrite     ^   https://$server_name$request_uri? permanent;
server {
    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;

As usual reload sudo nginx -t && sudo service nginx reload


certbot renew

Installing Mysql/MariaDB on a Rapsberry Pi

Here we will install MariaDB. The performance is better and it’s more open source. There are a few differences but nothing you will notice during normal daily use.

Installing mariadb:

sudo apt-get install -y mariadb-server-10.0

Alternative if you don’t want to get out of your comfort zone use mysql:

sudo apt-get install -y mysql-server

During this install you will get asked for a SQL root password. Choose a long secure password. You will need it later to make a database so also store it somewhere save.

After it’s done check the version with mysql --version. It should be 10.0.30-MariaDB or higher.

Creating a database and user

To create a database we are going to login to MySQL with the following command.

mysql -uroot -p

It will ask for the mysql password. After that the commandline will be different. We are going to create a database and a user and connect them.

CREATE DATABASE raspimain_db;

Next we create a user, be sure to replace the password!

CREATE USER 'raspimain_user'@'localhost' IDENTIFIED BY '%%SAFE_PASSWORD%%';

Then we need to connect the user to that database.

GRANT ALL PRIVILEGES ON `raspimain_db`.* TO `raspimain_user`@`localhost`;

Next we 2 commands are pretty self explanatory.


To test if it worked login in to mysql with that user.

mysql -u raspimain_user raspimain_db -p

This time use the password you used to create the user. Check if the raspimain_db is in the list of databases.


If it is use EXIT; to exit the mysql promt.