WordlePress and nerding out about WordPress function names

Given all the “Wordle” hype these days, I think I was one of the first (of many) people to wonder about what “WordlePress” might look like:

https://twitter.com/magicroundabout/status/1478999708228108293

Not long after, Taco Verdo did buy the domain, with the aim of preserving it for the good of the WordPress community.

Then, one bored weekend, I came up with the idea of making a Wordle-style game where you had to guess a WordPress core function name.

The prototype took a couple of hours to make. And it’s fine as a game in principle. But it’s REALLY hard.

You can play at https://wordlepress.netlify.app/ – I but I warn you, you need to be a proper back-end WordPress geek to be anything like any good!

Behind the scenes

I wanted to write some notes about the behind the scenes, how I made this work stuff. And, because I’m a geek, I got all curious about WordPress function names: how long they are; how common the different name parts are.

Let’s dig in!

Finding all the function names

The game is based on 4-part function names. “Parts” are separated by underscores. So get_attachment_image_url is a good example.

I needed to find all the global function names, count their “parts”, and filter them. So I needed to do some pre-processing of the WordPress code.

I got all the function names from the wordpress-stubs package. This is a package used for static analysis that contains all the class, and function signatures throughout WordPress core, but without the actual function/class code. All the function/method bodies are empty. And they are all concatenated into one file.

I used a combination of grep and cut commands to get all the function names from this file. Because the regex filtered out any functions with public/protected/private visibility specified, this should have ignored most class methods, and only given my globally available functions.

Here’s the command I used:

egrep '^\s+function\s+([^ \(]+)' wordpress-stubs.php | cut -d" " -f6 | cut -d "(" -f 1 > wpfuncs.txt

I then opened Tinkerwell to mess about with the data I had using my PHP skills. This just seemed like the quickest way to get to some interesting answers about the function names.

The thing I really wanted to know was how many function names had 3, 4 or 5 parts, as this would influence the game.

This isn’t clean, well-engineered code, but it does the job. I’m gonna use it once and throw it away so I’m not thinking too hard.

// Read in all the function names.
$funcs = file_get_contents('wpfuncs.txt');

// Keep counts of number of function name parts
$lengths = [];

foreach (explode("\n", $funcs) as $func) {
  if (! empty(trim($func))) {
    // Split the function name at _'s
    $parts = explode ('_', $func);
    // Discard empty parts
    $parts = array_filter( $parts, function ($a) { return ! empty($a); } );
    $parts = count($parts);

    // Update the count in the array
    if (isset($lengths[$parts])) {
      $lengths[$parts]++;
    } else {
      $lengths[$parts] = 1;
    }
  }
}

// Sort the array by number of function parts
ksort($lengths);

var_dump($lengths);

At the time of writing, this gives us:

[
     0 => 1, 
     1 => 42,
     2 => 576,
     3 => 1196,
     4 => 1004,
     5 => 420,
     6 => 182,
     7 => 62,
     8 => 18,
     9 => 4,
     10 => 2,
     11 => 1,
   ]

From the point of view of making the game, we see that 3-part functions give us the greatest number of functions with 1196 function names. This isn’t necessarily good though as:

  • Guessing more parts means we more quickly rule words in or out of the function name
  • So guessing fewer parts each time AND having more functions to guess makes the game harder.

The 420 5-part functions seemed too small though. So I decided to go with the 1004 4-part functions for the game.

It turns out this is STILL a very hard game!

Stats, functions and “fun”

This section has no real value at all. But I was curious about some of the numbers and functions here.

  • How many global functions are there?
  • What are the shortest and longest function names?
  • What are the most common “parts”/”words” in function names (apart from wp_)?

Let’s dig in.

Total global functions

According to this analysis there are 3508 global functions in WordPress.

That’s quite a lot!

Longest and shortest function names

In terms of function name parts/words, we can easily see:

  • There’s a function with ZERO WORDS? Bear in mind that underscores themselves don’t count. Maybe you can guess it? If not, head to the docs.
  • There are thirty-four 1-word functions. Perhaps you’ve used dbdelta, trailingslashit, mysql2date, absint, bloginfo, selected or checked? But what about saveDomDocument, zeroise (curious in its non-US English spelling), or antispambot?
  • There are 2 10-part function. Hands up if you’ve used wp_add_trashed_suffix_to_post_name_for_trashed_posts or the undocumented _delete_site_logo_on_remove_custom_logo_on_setup_theme?
  • And there is just one 11-part function. Bonus points if you’ve ever called block_core_calendar_update_has_published_post_on_transition_post_status for some reason (it’s also undocumented).

Most common words/parts

I’ll need to add some new code to analyse this. Here we go:

$funcs = file_get_contents('wpfuncs.txt');

$wordUses = [];

foreach (explode("\n", $funcs) as $func) {
  if (! empty(trim($func))) {
    $parts = explode('_', $func);
    $parts = array_filter( $parts, function ($a) { return ! empty($a); } );
  	foreach($parts as $part) {
      $wordUses[$part] = isset($wordUses[$part]) ? $wordUses[$part] + 1 : 1;
    }
  }
}

asort($wordUses);
var_dump($wordUses);

Sorting this way around (ascending order of use) isn’t particularly revealing. We just get all the words/parts that are only used in one function. Of which there are a LOT!

In fact, there are 1434 distinct words used in WordPress core global functions. And 563 (nearly 40%) of those words are only used in one function name! No wonder WordlePress is so hard! Perhaps we can analyse just the 4-part functions in a moment.

A quick array_reverse() gives us the most-used words/parts. And it’s no surprise that wp leads the way, appearing in 1245 functions. More than a third of all functions have wp in!

If you’re playing WordlePress you’ll want the top-ten for starter words. So these are:

  • wp (1245 instances)
  • get (firmly second with a massive 762 instances!)
  • post (third with 308 instances)
  • block (fast upcoming in 4th place with 217 instances)
  • link (166 instances)
  • is (156 instances)
  • core (144 instances)
  • update (138 instances)
  • the (133 instances)
  • add (124 instances)

WordlePress words/parts

So is wp_get_post_block the best opener for WordlePress?

Remember we’re only using 4-part functions. Is the analysis any different?

Well, it’s a little different.

The best WordlePress opening guess is wp_get_post_link

wp remains strongest by far. It’s in 482 of the 1004 4-part function names. Close to 50%!

core and block get demoted a bit. update drops down the rankings. And ajax, image and register climb into the top ten:

  • wp (482 instances)
  • get (300 instances)
  • post (115 instances)
  • link (62 instances)
  • ajax (54 instances)
  • block (52 instances)
  • is (45 instances)
  • the (43 instances)
  • image (41 instances)
  • register (36 instances)

But there’s still 763 words to choose from! Which is one of the reasons WordlePress is so hard.

Hopefully this new knowledge will help you play. Or maybe I’ll use it to refine the game to make it easier in future.

In any case, this was kinda fun weekend tinkering. I hope you learned something!