Category Archives: WordPress Bits

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' );

How to Become A Better Developer

I was on a panel at WordCamp Seattle this last weekend on becoming a better developer. One point I repeated a few times was “read core” and a related point of “don’t rely on tutorials.”

These ideas have been circling in my head since then, so I wanted to write them down.

Are Tutorials a Problem?

Tutorials can be great for providing context for code, but they often leave the reader without the tools to work out of that context. They may come away with working code, and even be able to reuse it over and over, but often without knowing what it is they’re doing. This happened to me.

It seems to me that a lot of tutorials fall into one of three categories:

  1. Installers: This is probably what irks me most. There will be a headline that looks like “How to foo bar” and the first step is “Download the Foo Bar plugin” and proceed to explain how to set up a plugin. These tutorials may be perfectly fine for helping a user solve a problem, but the title is very misleading.
  2. Copy-Pasta: These often just describe a problem and offer a snippet as a solution but with little-to-no explanation.
  3. Over-Simplified: These are tricky. The tutorial is often really trying to teach and does so understandably by simplifying the code to the bare-bones. But they are incomplete because they skip the ‘shoulds’ (like sanitization, and security) because they’re not ‘have tos’ so that users will end up with code that technically works but with no idea that there should be more.

I’m not sure there’s hope for the Installers. Those authors should just be more careful about picking titles. Or I should be better about detecting linkbait.

As for the other two, I won’t pretend I’ve never done those. I’m also not suggesting that they have no use, but I think there’s room for improvement.

Copy-Pasta is a great way to end up with conflicting function names, a bloated functions.php file, and/or unintended side-effects.

To me, the over-simplified ones are the most dangerous because the user doesn’t know what they’re missing. It’s like teaching someone to build a car, but never getting around to the seatbelts and windshield – they’ll get in and drive on the highway and be fine till they’re not.

Go To the Source (Code)

If you want to learn how a function really works, you’ll have to look at the source. Tutorials can provide examples of how to use a function or hook, but you’re often on your own to learn how/if you can repurpose or reuse it in a different context.

I’ve faced this personally more than once, and wrote about it.

WordPress core is neither magic nor mystery (though it can feel like it at times!). It’s full of awesome gems that you may never learn about unless you dig in.

Where do you start? Try the Code Reference and look up functions that you see in tutorials. Personally, I spend a lot of time in my xref or just straight up opening core files in my text editor.

Turn That Snippet Into A Plugin

This post is sort of a recap of a talk I gave a couple years ago.

Whenever you get a WordPress code snippet, where do you put it? It’s not uncommon all those bits of code to end up cluttering up the functions.php file of your theme, whether out of habit or suggested by tutorials.

But wouldn’t it be better if all those non-theme snippets were plugins instead? Yes!

As plugins, you can switch/upgrade themes without losing that functionality and toggle the funtionality without editing the theme.

And great news! It’s really easy to turn a snippet into a plugin.
(more…)

Nginx, robots.txt, and Copy-Pasta

Did you know that if you don’t have an actual robots.txt file, WordPress will create a virtual one for you? For example, I have not created a robots file for trepmal.com, yet you can see one at https://trepmal.com/robots.txt.

So either you have created your own file, or you’re relying on the virtual one. Unless you’ve explicitly disabled WordPress virtual robots.txt file, you’ll have something at yoursite.com/robots.txt

However, if you (1) use nginx, and if you (2) followed certain popular guidelines* for configuring your site, and if you (2) are relying on the virtual file, you might discover that you get a 404 if you try to view your robots file.

(more…)

WP-CLI in the Real World

I’m looking to spread the word about WP-CLI. One way I’ve decided to do this is to submit a WordCamp talk proposal on the subject.

Of course I can talk about how I’ve benefitted and about my favorite things, but I’d really like to make sure my talk is well-rounded and takes into account real world usage. Basically, I would love to hear from you about your experiences with WP-CLI.

  • Biggest hurdle to overcome?
  • Favorite command?
  • Most irritating aspect?
  • That feature you’re looking forward to using?
  • When did WP-CLI save you hours on a project?
  • Did it teach you something?
  • something else?

Whether there’s something you want to learn or a tip to want to pass on, let me know in the comments!

Getting Started with WP-CLI

By getting started, I mean really just getting started. I’m just going to show you how to get WP-CLI running. I think once you do, you’ll take off on your own.

If your local development environment is on VVV, then stop reading, you’ve already got WP-CLI installed!

Here’s a summary of what we’re going to do:

  1. Download the application
  2. Make it executable from the command line
  3. Move and rename it
  4. In case of XAMPP (or MAMP), add its binary directory to PATH (need to make sure XAMPP’s php is used, and not your machine’s)

Yes, the instructions for installing WP-CLI can be found on its site, but sometimes I just like to reiterate and expand.
(more…)

The publish_post hook

The publish_post hook is one I’ve seen a lot in tutorials and such. Even has its own page in the codex.

But surprise! That hook doesn’t actually exist, at least not if you search for do_action\(\s?.publish_post.

When I was learning to dig into core, this baffled me. What was this publish_post sorcery? Why wouldn’t it work on my custom post types???

Here’s a secret: The actual hook in core looks like this "{$new_status}_{$post->post_type}". It’s part of a small function in wp-includes/post.php (line 3321 at the time of writing), and there’s another equally useful variable hook right before it.

function wp_transition_post_status($new_status, $old_status, $post) {
	do_action('transition_post_status', $new_status, $old_status, $post);
	do_action("{$old_status}_to_{$new_status}", $post);
	do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}

There are quite a few of these variable hooks in core, I encourage you to go look at them, here’s a list I scraped together:

(more…)

Stronger than dirt… I mean Ajax

I first learned about Ajax at a WordCamp (yay!), but I didn’t fully grasp how to implement it. There was a helper script provided, and I figured out how to make it work for my plugins, but all I really knew was copy & paste.

It was at least a good 6 months before I dug in and looked at the WordPress Ajax API. Getting a closer look helped me understand what I was writing, and how I could simplify and/or make improvements when necessary.

So let’s start.

(more…)