The order of arguments in an if statement

Consider this dummy snippet:

<?php
if ( true === $skip || heavy_check() ){
    continue;
}

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

<?php
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 taxonomy 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.

<?php
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

[^a]*

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

Bash script get own path and name

pathfile.bash

#!/usr/bin/env bash

# Full path to file
echo "$(cd "$(dirname "$0")"; pwd)/$(basename "$0")"

# Full path of the directory the file is in with trailing slash
echo "$( cd "$(dirname "$0")" ; pwd -P )/"

# current directory name, not the path
echo "${PWD##*/}"

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 still out of my comfort zone (update dec-2019: still is).

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 handful of commits reapplied. Otherwise you will need to find an other way.
Or cherry pick a lot…

This blogs blueprint

How is this blog build. What theme, plugins and such.

Update dec-2019:

This article is now completely outdated.

Original:

I’ve had the idea for a blog for years. But actually bloging keeping it up is hard. So here is another go. This blog had no restrictions for me. So when setting this up I did some things I would(/could) never do anywhere else.
The biggest sin I commit is not having a development environment.
So how I this blog (being?) build:

Continue reading “This blogs blueprint”