As nice as event-driven programming can be within the context of WordPress’ hook system, one of the challenges is preventing code from executing every single time the hook is called.
For example, say you’re writing a function that fires during the init action but something happens in WordPress core that triggers the init action to fire again consequently causing your code to fire once again even though it’s unneeded.
Multiply this across however many callbacks spread across however many files and general functionality registered with the hook and you may end up affecting performance and/or executing code that has no need to be run.
Static Variables in Plugin Bootstrap Files
One way this can happen is in a plugin’s bootstrap. Case in point: Say your plugin is registered with the init action and then it sets up a registry which in turn instantiates a set of subscribers that register services. Repeating this every single time init is fired is unnecessary.
Here’s a way to manage this:
add_action( 'init', 'tm_acme_function', 100);
function tm_acme_function() {
static $initialized = false;
if ( $initialized ) {
return;
}
$initialized = true;
// ... set up the rest of the function.
}
If you know how static variables work, you’re may already be doing this, you’re able to follow the above code, or both. And if that’s the case, there’s nothing else to see here.
But if not, static variables can be useful in this scenario because static variables maintain state between calls whereas regular variables are reinitialized every time the function fires. This means a static variable retains its value across multiple calls to the function.
Static Variables and Plugins
So having an static $initialized flag works like this:
$initialized starts as false.
When the function runs for the first time, the $initialized variable is set to true.
On subsequent calls, the condition if ( $initialized ) prevents the rest of the function from executing, effectively short-circuiting it.
And because of that, this:
prevents redundant execution,
optimizes performance by avoiding running unnecessary code (especially as it relates to registering duplicate functionality, running multiple queries, or trashing data unintentionally).
If your plugin’s bootstrap registers a callback with a WordPress hook, considering using static variables to prevent code from being called unnecessarily more than once.
To say that 2024 has been a year would be an understatement. Though I’m talking about things that have happened offline, the same can be said for the WordPress economy at large, too.
On a regrettable level, the degree at which I’ve written has decreased more this year than likely any other year since I’ve been writing. Some of this can be attributed to stage of life, some can be attributed to work, and some of this can be attributed to the rise of AI in our industry.
Though the goals of this question are not mutually exclusive, I think getting an answer fast often outweighs the “I’m looking for an answer but it was neat to also read about someone else’s situation while searching for it.” And this is why ChatGPT has “wrecked” some of the content a bunch of us typically write.
But, as stated, it’s been over a year since this was written. And since I work in R&D in my current role, we’ve done – and continue to do – a lot of work with the various systems, applications, utilities, and so on.
Given that, I – like many of you – have recalibrated my perspective on how this changes the work we do.
ChatGPT Didn’t Wreck Our Type of Content
Improved Productivity
First, it’s undeniable that when used properly, AI assistants can vastly improve productivity. I run both Copilot and Cody in my editor as I’m consistently evaluating which one performs best for a given use case. At the time of this writing, I’m partial to Cody though I also know Copilot is going to support multiple LLMs in the coming months (or weeks?).
So, sure, AI assistants have changed the way the work in our day-to-day but, as the months have passed, I’m no longer convinced it’s “wrecked” our type of content so much as it’s “drastically altered” how we explain – for lack of a better word – our content.
One of which is more neutral than the other.
Large Context Windows but Lacking Context
Secondly, for as much as I typically work with ChatGPT, Gemini, and/or Claude (is there a clever acronym for all of these, yet?) on a daily basis, I find myself continuing to enjoy well-written content either in newsletters (see The WP Minute, The Repository, or Within WordPress) or blogs (see Brian Coords, what Mike is doing over with Ollie, and so on). Though I’m but one person, each of these properties or people continue to publish even though LLMs are available for any of us to use.
And that brings me to the final point: There are reasons AI hasn’t completely wrecked the type of content I – and others – have often published:
AI hallucinates. Recommendations provided within a given LLM are presented with an authoritative sense regardless of if the recommendations even use hooks, function names, or language features that don’t exist.
Lack of context. LLMs do not have the context as to how a given developer arrived at a solution and why one was chosen over another. Sure, you can ask for a variety of solutions and tradeoffs but there are times in which it’s still faster to read from someone who’s had the same experience, shared it, and provided contextual information as to how and why they arrived at a solution.
Aggressive Autocomplete. I’m a fan of using coding assistants within my IDE. As I said, the level of productivity and speed of solving problems has definitely increased, but that doesn’t mean its attempts to autocomplete a piece of functionality are always helpful. It still takes a critical eye to review what’s being proposed and determine whether or not it’s worth integrating.
There are likely more and your experience likely varies – but I suspect aren’t much different – from mine.
The Why Behind the How
The reason I share all of this is because one of the fundamental things that is missed when working solely with AI is the value that human beings bring to the table when sharing the why behind the how.
This is not me taking a position on whether or not AI will, can, should, or whatever other argument is the current hot topic replace humans. Instead, it’s me saying that although I appreciate the value AI has brought to our industry and I recognize it alters the need for certain types of content, I no longer think it completely negates or replaces the type of content about I – and others – used to write.
Sure, our approach may need to be tweaked but there’s still plenty of ways to regularly share what we’re working on, how to solve a certain problem, and why one solution was chosen versus another.
Finishing 2024, Into 2025
Given that 2024 is coming to a close in the coming weeks and that we seem to have accepted the role AI plays in the day-to-day work of software development, perhaps I can start writing somewhat regularly once again.
There’s no shortage of things I’ve built, learned, saved, and archived. And while others have continued to publish their stuff, I’ve missed doing the same. Perhaps the coming weeks – and coming year – is a time in which those of us who so frequently wrote about development can find our way to back to doing exactly that.
TL;DR: I often create playlists for entire albums within my Apple Music library. This means that if an artist releases an album, I’ll still add it to my library, but I like to have a playlist of just that album. Unfortunately, Apple Music doesn’t use the album’s artwork as the image for the playlist.
So I wrote a Python script to fetch album artwork for Apple Music playlists when given an artist and an album. This makes it a little bit easier to make sure album playlists have better looking artwork.
Fetch Album Artwork for Apple Music
Whenever we create playlists within the current version of Apple Music, it gives us the ability to create playlist art that seems to be inspired by the Microsoft WordArt of decades past.
It’s certainly a choice. And for whatever reason, Apple Music doesn’t default to the album artwork for the album in the playlist if it’s just a single album.
Apple Music does allow us to add custom artwork, though. So if I want to add the album artwork for a given album in a playlist, then I can search the web for a high-resolution image and drop it into the editor for the playlist.
Perhaps a better option, though, is to grab the image artwork from the iTunes API. So I wrote a Python script that makes it trivially easy.
After it’s installed, all you need to do is issue the following in your terminal:
$ python fetch-album-art.py --artist="Pink Floyd" --album="The Dark Side of the Moon"
This will grab a high-resolution image of The Dark Side of the Moon and drop it into the script’s directory after which you can add it to Apple Music.
Future State
Assuming Apple doesn’t actually address this in a future version of Apple Music (which, if history is any indication, happens with each official macOS update), then this is something that’ll prove useful [at least for me] for the next little while.
That said, I’d still like to find ways to enhance the functionality so:
album artwork is added to its own directory,
considering combing through an Apple Music playlist library to determine what albums don’t have artwork (this is subject to the limitation of the API, obviously),
provide better visual feedback when the script is searching and downloading the album artwork,
and small tweaks like that.
Given that it’s a hobby project, though, it’s one of those that’s subject to the whims of when I want to work on it.
For now, though, this is still one step better than having to find an image via a web browser, download it, then drag it into Apple Music.
TL;DR: If you’re using WordPress 6.1+ and aren’t able to schedule any type of job with Action Scheduler, this article explains the problem and a potential fix.
Specifically, this seeks to address the following message appearing in WordPress 6.1+:
Cron reschedule event error for hook: action_scheduler_run_queue, Error code: invalid_schedule, Error message: Event schedule does not exist., Data: {"schedule":"every_minute","args":["WP Cron"],"interval":60}
Resolve the Reschedule Event Error
Introducing More Logging
Starting in WordPress 6.1, additional logging was added to WordPress core. Specifically, the patch responsible for this includes the following description:
Rarely and randomly some of my custom cron events have disappeared. From searching around, I’m not the only one with this issue, and no one else was able to figure out why. I also was unable to debug the issue since wp-cron.php doesn’t log any errors nor have any hooks to try handling them. This patch adds those in.
Once this patch was introduced, it also resulted in others experiencing issues with cron and cron-related libraries starting from this ticket and then taking place in a specific forum post.
And sure, these tickets are helpful as are the comments and the rest of the discussion in the forum. But there are times when we’re working on a specific task with a specific set of dependencies and need a specific solution.
Action Scheduler and Cron Jobs
My problem was this: I was trying to to register a job using Action Scheduler and the library wasn’t able to register the schedule because of the aforementioned problem.
So the fix was to add this function in my code:
/**
* Additional logging in WordPress 6.1+ that generates the following
* message regarding cron schedules:
*
* Cron reschedule event error for hook:
* action_scheduler_run_queue,
* Error code: invalid_schedule,
* Error message: Event schedule does not exist.,
* Data: {"schedule":"every_minute","args":["WP Cron"],"interval":60}
*
* This function needs to fire prior to loading Action Scheduler as its a
* pre-requisite for it to schedule our tasks.
*
* This filter seeks to manually add the schedule to the list of schedules to
* address this bug.
*/
add_filter('cron_schedules', function ($schedules) {
$schedules['every_minute'] = [
'interval' => 60,
'display' => 'Every Minute',
];
return $schedules;
});
A few things about this code:
I don’t recommend using this as a permanent fix for every case. It’s a specific solution for a generic warning.
If your codebase is going to be distributed to wide audience, avoid anonymous functions. If you have control over the environment in which it will return, it may be fine.
On the other hand, if you’re using Action Scheduler, WordPress 6.1+, and are trying to register your own jobs and are seeing this message, this will ensure schedules Action Scheduler uses are available.
Kinsta offers premium managed WordPress hosting for everyone, small or large. Powered by Google Platform and strengthened by Cloudflare, they take performance to the next level. All hosting plans include 24/7/365 support from their team of WordPress engineers. Get startedwith a free migration today.
If you are looking for WordPress guides and unbiased reviews, I recommend checking out WPKube. They also have an exclusive deals section.
Just Getting Started with WordPress? I write a lot about WordPress development but if you're just getting started, I recommend checking out WPBeginner. They have free training videos, glossary, and more.