Ambiguous Relative Dates

Using relative dates like “next Thursday” can be confusing. (Is that this Thursday? Then why didn’t they say “this Thursday”?)

Try these polls for fun:

Does removing the weekend change things?

 

 

Cookie-Nonce authentication for REST API cURL Requests

The WordPress REST API is quite a feature, but it can be a struggle to deal with authentication. One option is Basic Auth. But can we leverage the built-in cookie authentication?

If you look at rest_cookie_check_errors(), you’ll see where it’s checking for an authentication cookie and valid nonce. Using WP-CLI, we can carefully piece together a cURL command that passes those values appropriately. Remember, cookies and nonces have a limited lifetime, so be sure to generate new commands as time goes by.

Things I’ve learned by working from home

I used to enjoy working from the couch…

A post shared by Kailey Lampert (@trepmal) on

I’ve been working from home full-time for most of the past 4.5 years. I haven’t reached any special milestone, but nevertheless I’m going to compile a list of some of the things I’ve learned, some bits of advice, and general observations. In no particular order, numbered for reference:

  1. Not commuting is awesome
  2. Sometimes your car battery will die if you don’t drive for a while
  3. I’m possibly too comfortable not leaving my house for days on end
  4. Getting a dog is probably healthy
  5. Playing fetch is a good way to get out of the house when you don’t really want to do anything
  6. I don’t worry about someone stealing my packages
  7. I’m pretty sure the UPS man thinks I’m unemployed, and possibly a recluse
  8. It’s easy to forget you’re still wearing pajamas at 2pm
  9. Comfy slippers are worth it
  10. Roosters are loud
  11. If you leave a window open, people on your video call are likely to hear some crowing, or even mooing
  12. Pets have no concept of work hours
  13. Pets have no concept of personal space
  14. A cat is likely to send at least one Slack message
  15. Check for nearby cats before entering any commands in a production terminal
  16. If you can’t be work productive, be personally productive
  17. A decluttered desk can mend a cluttered mind
  18. If you’re in the zone, don’t worry about the 4 coffee cups you’ve acquired next to your keyboard
  19. Someone in your life will believe you’re unemployed no matter how many times you tell them you work, just at home
  20. It’s easy to use “sorry I’m working” as an excuse to avoid social events, even on evenings and weekends
  21. Don’t do that. At least not too often
  22. It’s hard to take a sick day unless you’re at a high risk of literally vomiting on your keyboard
  23. Hardly anyone steals my lunch
  24. Sometimes I don’t eat lunch as much as snack between breakfast and dinner
  25. Taking an actual lunch break is hard
  26. Don’t forget to wash that hoodie
  27. Regular video meetings, even if sometimes unproductive work-wise, are good for recalibrating your teammates’ sarcasm levels
  28. Sarcasm is likely to backfire once in a while in text based communication
  29. Emoticons and emoji can actually be helpful in setting tone
  30. Imposter syndrome is very real
  31. People you look up to are also asking so-called stupid questions, they’re just in one of the other 2839416 Slack channels you haven’t stumbled into
  32. Don’t forget to stretch
  33. Sometimes the scenery gets boring
  34. Working from a coffee shop is tricky
  35. Working from the couch is cozy
  36. Mind your posture
  37. A second laptop charger is worth it
  38. No rules against sitting outside
  39. Working from a hammock is logistically challenging
  40. It’s hard to ignore work stuff during off-hours
  41. I’ve saved so much money by not commuting past fast food and coffee stands everyday. Oh, and the gas…
  42. Drink more water
  43. It’s hard to take “unnecessary” days off, but they are necessary
  44. I bet real office chairs are nicer than hard plastic, I should consider this
  45. The less you drive, the more handy a key hook is
  46. You’ll forget how tall/short your coworkers are
  47. Document everything
  48. I will never be able to convert UTC to PST in my head
  49. Yes, I know it’s just add 8 hours
  50. Video chat will never remember the proper audio/video settings

Command Line Shortcuts

Some command line shortcuts that might make your life easier. They’ve helped me, but sometimes I forget so I decided to group some of my favorites here.

To be specific, we’re talking bash. As a common default shell, there’s a good chance it’s what you’re using. So a lot of this can be found inside the bash manpage. If you’re using something else, I can’t say what of this might be compatible…

Event Designators

Ever need to repeat the last command? Easy, just use !!

$ echo some super-duper-annoy-to-retype command
some super-duper-annoy-to-retype command

$ !!
echo some super-duper-annoy-to-retype command
some super-duper-annoy-to-retype command

$ echo "!!" > what-was-that.txt
echo "echo some super-duper-annoy-to-retype command" > what-was-that.txt

$ cat what-was-that.txt
echo some super-duper-annoy-to-retype command

This is extremely handy if you meant to run the last command as sudo, just run sudo !!
(more…)

Notes: Renaming and relocating directories in WordPress

This is not a guide (yet?). These are just notes for personal reference which may be expanded upon later.

Site owners/developers/administrators may find useful hints below, but please do not get mad at me if you break your site.


Methods were initially tested on a multisite-with-subdirectories installation, but are generally applicable to single and subdomain installations as well.

standard installation

familiar structure, e.g.

├── wp-config.php
└── wp-content/
    └── plugins/

nginx (subdirectory multisite)

if (!-e $request_filename) {
    rewrite ^(/[^/]+)?(/wp-.*)   $2                   last;
    rewrite ^(/[^/]+)?(/.*\.php) $2                   last;
}

(more…)

WP CLI: --skip-plugins

A few notes on skipping plugins with wp-cli.

Suppose you want to skip all except one, DONTSKIP:

wp user list --skip-plugins=$(wp plugin list --field=name | grep -v ^DONTSKIP$ | tr  '\n' ',')

If this will be repeated, you’d benefit from saving the ‘skip-list’ to a text file instead of running the nested command each time. This also allows you to update the skip-list relatively easily, if you don’t mind tighly squeezed comma-separated lists.

wp plugin list --field=name | grep -v ^DONTSKIP$ | tr  '\n' ',' > skipplugins.txt
wp user list --skip-plugins=`cat skipplugins.txt`

If it’s a permanent skip-list, save the keystrokes and processes by putting it in a config file (different options depending on how global you want that change: http://wp-cli.org/config/)

 #~/.wp-cli/config.yml
skip-plugins:
 - skip-me
 - skip-me-too

isset() Assumptions

register_setting() can be really handy, but take note! The first time your option gets saved, it’ll pass through the santize callback twice. With most input, it won’t matter; and if you’re explicit, it won’t matter either. But under the right conditions, it will.

Suppose your register_setting() looks like this:

register_setting( $option_group, 'checkbox', 'sanitize' );

And you have a pair of checkboxes like this:

<input type="checkbox" name="checkbox[one]" value="1" />
<input type="checkbox" name="checkbox[two]" value="1" />

And finally, your sanitize callback looks like this

function sanitize( $input ) {
	$input['one'] = isset( $input[ 'one' ] ) ? true : false;
	$input['two'] = isset( $input[ 'two' ] ) ? true : false;
	return $input;
}

See the problem?

On the very first save, both $input['one'] and $input['two'] will be true, no matter what. Say checkbox one is unchecked, when it fist passes through, $input['one'] is set to false – as expected. But when the input is passed through sanitize the second time, $input['one'] is set (to false) and thus the $input['one'] is changed to true.

demo

So the moral of the story is: be more explicit, don’t make assumptions.

Fun with WP-CLI

A random collection of things you perhaps didn’t know you could do with WP-CLI.

Maybe you’ve imported an image before, but did you know you can import a whole directory just as easily? For example, if the directory is named ‘images’:

wp media import images/*

Sometimes you need to run a command based on a result set from another command. In many cases, you can do that by nesting the one in the other. For example, if you want to change the password for all users with the ‘author’ role.

wp user update $(wp user list --role=author --field=ID) --user_pass=password

Or go crazy and regenerate media for the featured image of all sticky posts.

wp media regenerate $(wp eval 'foreach( wp_parse_id_list( get_option("sticky_posts") ) as $id ) { echo get_post_thumbnail_id($id). " "; }')

Trying to run a command for each site in a multisite doesn’t have to be a chore. A little bash script can speed things along.

#!/bin/bash

for url in $(wp site list --field=url)
do
	wp theme activate twentyfourteen --url=$url
done

Save that to a file, and run with bash: bash filename

You can do that in a one-liner as well, but that can make it harder to see what you’re doing, especially with more comprehensive commands.

for url in $(wp site list --field=url); do wp theme activate twentyfourteen --url=$url; done

Function: wp_list_pluck()

Suppose you want to get some information about some posts in this format: an array of post titles, keyed by the post ID. How would you do it? Spoiler alert: Skip the first 2 options

Let’s assume you’re fetching some posts via get_posts(). For simplicity I won’t be passing an arguments to that function, but go ahead if you want to 🙂

$posts = get_posts();

Option 1

$post_titles = array();

foreach( $posts as $p ) {
	$post_titles[ $p->ID] = $p->post_title;
}

Not bad, but surely there’s a WordPress-y way to do that.

Option 2

You might have come across the function wp_list_pluck() which is really neat for pulling a particular value by key from an array of arrays or objects.

$post_titles = wp_list_pluck( $posts, 'post_title' );

This works for getting the titles, but leaves us without the IDs as keys. You might extrapolate that to this

$post_titles = array_combine( wp_list_pluck( $posts, 'ID' ), wp_list_pluck( $posts, 'post_title' ) );

Now we have what we want, but that seems clunky.

Option 3 (the best!)

Since WordPress 4.0, wp_list_pluck() takes a third argument that makes it behave more like the native PHP function (since 5.5) array_column(). This third argument allows you to specify a key whose value should be used as the key of the returned array.

$post_titles = wp_list_pluck( $posts, 'post_title', 'ID' );