wp cli output format with –porcelain

wp db export

Will output something like Succes: Exported to 'dbname-2018-09-16-2790c11.sql'
Often you need the filename.

--porcelain flag to the rescue!

wp db export --porcelain
dbname-2018-09-16-2790c11.sql

This will work on a lot of commands that have one item output.
Stuff like wp post create ....
Here is a complete list of commands that have the –porcelain flag

Commands that output more items usually have a --format flag to handle output.

Declare a variable to a bash script runtime

In the past I’ve did a post about checking if a bash argument is set.
This is different, This way you can pass through a named variable.

    TEST='pass it on' ./testscript.sh

If you do this $TEST will be set inside the ./testscript.sh bash script.
Like the normal arguments you probably would like to validate them inside the bash script.
The main plus side is that you can put arguments in any order and exclude arguments.
It might also be easier to read as you can see the arguments name.

A bit more advanced. You can override global variables for that script run.
Variables like $PWD and $PATH, be careful.

Example script: ./testscript.sh

#!/usr/bin/env bash

if [ ! -z $TEST ]; then
    echo "var is set to '$TEST'";
else
    echo "var is unset";
fi

Global git ignore

A new laptop a new change to optimize stuff. And this is one I should have done years ago.
There are files you always want to avoid. The infamous .DS_Store file comes to mind.

On ubuntu just add whatever you want to exclude to ~/.config/git/ignore

Copy files/folders over ssh

Sometimes you just want to copy files from a remote server, and you want it often and quick. Stuff like a DB dump.
You could startup your (s)ftp client and browse to it. Or you could just grap it with ssh.

Download one file

scp -user@hostname.com:/full/path/to/dump.xml /local/path/to/Downloads

Download a whole folder

scp -r user@hostname.com:/full/path/to/folder /local/path/to/Downloads

Composer based on path

I wanted to include one directory form an svn repo.
With help of stackoverflow I found the solution.

"require-dev": {
    "wordpress/phpunit": "*"
},
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "wordpress/phpunit",
            "version": "1.0.0",
            "source": {
                "url": "https://develop.svn.wordpress.org",
                "type": "svn",
                "reference": "trunk/tests/phpunit/includes/@43534"
            }
        }
    }
]

There is a downside, it won’t update. Unless you up the version inside the repositories block.

Bash edit command

I like doing a lot in one command. Downside it that stuff can get messy. One big line. you can’t easily edit it.
A shortcut to make it easier is ctrl-x e That will open the current command in an editor.

Few thinks to keep in mind.

  • Multiple lines will work, but it will execute it as separate commands.
  • using && at the end of lines will fix that.
  • Which of course means you get it in the history at full.
    My main use to chain commands.

Meta query order

The site had 20K plus posts/pages/ect, freshly put live.

The hoster called. It had slow queries, 9 seconds on average. A lot, multiple per minute. The same query over and over again.
It was a pretty normal post table query. It had one part extra.
A meta query was to exclude all spot_closed posts. When I removed that it was quick. It didn’t show anything special.

The meta query was added as such:

$meta_query_spots = [
    'relation'     => 'OR',
    [
        'key'     => 'spot_closed',
        'value'   => 1,
        'compare' => '!=',
    ],
    [
        'key'     => 'spot_closed',
        'compare' => 'NOT EXISTS',
    ],
];

Check if the spot is closed, and if it a spot does not have a close status.
The fix, flip the checks. And bam query less then half a second.

wp-cli eval-file

wp-cli can be used to execute a php file with the eval-file command.
Biggest plus; WordPress is loaded when you execute that file.
The point it will call this file is after the init action and after the later wp_loaded action.

WordPress can’t use number as page names

Where is what we wanted. A page example.com/404
So we had a page in WordPress so the content was controllable.

But on save the slug was changed to 404-2.

I figured maybe that slug was preserved. So after some digging I found this beautiful *ahem* if statement.

if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { //.......

The part it fails on is the preg_match. No number is accepted.
So it’s not just the slug 404 but every number.

The reason for this because it preserves numbers for pagination (including in-page pagination).

So the solution was to rename the page to example.com/404-page

Source: wp_unique_post_slug

WordPress Action Priority

I wanted to add_action between 2 different actions.
These actions where priority 3 and 4. So I tried 3.5. That didn’t work.

Decimals get rounded down. What did work was the string “3.5”

This got me curious about strings and the sorting in general. Under the hood WP just uses array_keys
To make things easier here is a list of use cases:

WordPress action how a priority is sorted