Tom McFarlin

Software Engineering in WordPress, PHP, and Backend Development

Page 3 of 423

Identifying and Registering Anonymous Functions in WordPress

Originally, this article started as an explanation for how to remove anonymous functions registered with WordPress. As I started writing, it became obvious it’s helpful to understand what PHP does behind-the-scenes when anonymous functions are registered.

This dude clearly doesn’t understand anonymous functions. The eyes give it away.

Furthermore, this helps give a deeper understanding as to why registered anonymous functions can be problematic and how we can deal with them on a technical level.

Finally, it’s worth noting that I don’t take a hard stance on if anonymous functions should never be used or not. There’s a time and a place where it makes sense, and there’s a time and a place where it doesn’t need to be done. That’s a topic to cover in the future, though.


I’ve mentioned this before, but I’m not in the business of sharing the author of any given tweet [anymore] for the sake of how X/Twitter mobs can behave. Besides, it’s the content of the tweet that warrants a discussion, not the person who said it.

Some time ago, I saw this come across my feed:

I do wish developers would stop using WordPress hooks with (what I think are called) anonymous functions […].

They are very hard if not impossible to unhook.

As mentioned, the how to deregister anonymous functions and the “whether or not this is a good idea” are topics for another post. For now, I want to share what happens whenever an anonymous function is registered with a hook in WordPress.

Named Functions in WordPress and PHP

Brainstorming names for named functions. Naming things is hard.

First, an example:

add_action('admin_init', 'tmAddBackgroundColor');
function tmAddBackgroundColor()
{
    echo '<style>
            body {
                background-color: #f3f3f3;
            }
          </style>';
}

In the code above, we’re using a named function – as opposed to an anonymous function – to do the following:

  • add_action('admin_init', 'tmAddBackgroundColor') hooks the tmAddBackgroundColor function to the admin_init action. It tells WordPress to execute the specified function when the admin area is initialized.
  • The actual function adds a custom CSS style to the admin area.

If I was a third-party developer who wanted to remove this functionality, then I’d locate the function and invoke the following call somewhere in my code:

remove_action('admin_init', 'tmAddBackgroundColor');

This is an obvious benefit to named functions. Even if the function is located in a different namespace, it’s possible to remove the action by prefixing the function name with the name of the namespace.

What happens with anonymous functions, though?

Anonymous Functions in WordPress and PHP

You don’t have to name an anonymous function. You might as well just stare at a wall.

If you’re familiar with anonymous functions, then the following code should be clear. If not, though, take a look and I’ll explain what’s happening after the block:

add_action('admin_init', function () {
    echo '<style>
            body {
                background-color: #f3f3f3;
            }
          </style>';
});

Anonymous functions are generally useful when you have a small, one-time callback that doesn’t need to be referenced elsewhere in your code.

Obviously, the functionality is the same, but it raises the question: How do we call remove_action on this function? That’s something we’re working towards, but first, here’s what happens in PHP whenever you’re working with anonymous functions.

PHP Closures, Closure IDs, and Registration in WordPress

An ID registered for a conference.

Before reading any further, note that a closure in PHP is a way to create an anonymous function.

Just like what’s demonstrated above, it allows us to write a piece of code that can be registered with a hook or, more generally, passed around as a variable. The latter part of this will make sense after we look at what PHP does when an anonymous function is created.

PHP does two things after you register an anonymous function with a WordPress hook:

  1. Generates an ID for the closure. To generate a unique identifier for this closure, WordPress uses the spl_object_hash() function.
  2. Associates the ID with the callback. The generated closure ID is then associated with the callback function, allowing WordPress to track and manage callbacks even when they are anonymous functions.

Given this, another way to write and associate an anonymous function with WordPress would be like this:

$adminInitCallback = function () {
    echo '<style>
            body {
                background-color: #f3f3f3;
            }
          </style>';
};

add_action('admin_init', $adminInitCallback);

And we can get the ID of the closure as generated by PHP by passing the variable to the spl_object_hash function. For example:

$closureId = spl_object_hash($adminInitCallback);

And that will return the ID that’s generated by PHP and associated with the specific hook in WordPress.

But Still, Deregistering Anonymous Functions?

Though this doesn’t help explain how to deregister anonymous functions, it does help explain how things work within PHP.

This will make it easier to explain ideas around anonymous functions ands hooks in a future post.

Meetings Are Inevitable (Quality and Frequency Vary)

TL;DR: If you’re looking at a career in software development, it’s imperative to know that meetings are inevitable. However, the frequency and quality of said meetings vary.

Don’t let the opportunity to ask potential employers – or current employees – what meetings are like pass you by whenever you’re interviewing.

Further, if you’re in a place that you feel has too many meetings or something about them could change, don’t be afraid to bring this up when the time is right.


In the original article, the author writes:

There are recurring meetings scheduled on a daily or weekly basis. Most of these are not productive. The majority of them are forced by a person who is organizing them because that’s the only “work” that that person is doing.

Meetings Are Inevitable

The short answer is yes, this is true. But it’s only true in some cases; it’s not the law of companies that hire software developers. Instead, it’s based on a number of factors (some of which do include people finding their measure of productivity based on how many meetings they can schedule – but I digress).

Speaking of those few factors, these are the ones I’d like. Some of them are related, some aren’t:

  • the size of the organization,
  • the size of the team,
  • the manager of the team,
  • how the project is managed,
  • and so on.

If you’ve worked in software for some amount of time, you’ve likely experienced this and/or you have your own item to add to this list.

A Word About Project Managers

One caveat I want to mention is in all of the years of working in software development, one of the things that I’ve seen – especially in a consulting or an agency-type of setting – is that a certain breed of software developers don’t enjoy having a project manager.

Assuming the project manager is worth the weight of their role and title, I’ve found having them to be liberating. It frees me, as the engineer, up from having to thing about certain things related to external factors and let’s me focus on exactly what I want to do: Write code to ship features, fix bugs, and build software.

A project manager that not only tracks tasks for software, but works on mathematical proofs in her free time.

Do not dismiss project managers. Instead, embrace them as the gatekeepers of sorts of the backlog of features and trust them to know what works best for the business. They are the liaisons between those who write code and those who will be using, or selling, the product. It lets us focus as much as possible on what you do well.

Current Status

If you don’t read anything else in this article, know that meetings do not have to take up the majority of your time when working in software development.

Currently, I work for a fully remote organization with several hundred employees. My team consists of, at the time of this writing, about seven people, and we have one standing meeting per week.

Standing meetings are not like this. Especially in a remote company. This looks like a scene from Silicon Valley.

We use almost never use email (and if you’re interested in moving in this direction, I highly recommend reading A World Without Email), primarily use Asana for project and task management, and have daily stand-ups through a morning report in Slack.

Though this works really well for me, it may not work for you; however, I do find that less email and fewer meetings helps to foster a greater sense of focus.

Other Factors

It’d belabor the point to break down all of the various different reasons as to how the size of an organization, the size of a team, or how management can influence the number of meetings.

Suffice it to say that I’ve worked for large organizations, medium organizations, and small organizations and each of them had their respective way of conducting meetings. I do think that, especially in the world of enterprise software development, there are a lot of meetings.

Ultimately, I’d say that if you’re currently looking at a career in this field and you’re thinking it’s primarily going to be sitting at a desk writing code, that’s misguided (and unfortunately so).

I guess everyone is in the conference room for a meeting that could’ve been avoided if only they’d had a proper project manager and expectations for their meeting.

Further, it’s a disservice many places that provide an education in software development often lack a component for what to expect when you’re actually pursuing a career in the field.

So what’s a person to do? If you have the opportunity to participate in an internship, a co-op, shadow someone in the field, pursue an apprenticeship in the field, or even just interview a number of people in the industry, I’d recommend doing any of the above.

Anything that will get you as close to the field as possible before jumping into it will be beneficial.

Ultimately What, Then?

I’m in no position to say how a business should be run nor do I have the hubris to prescribe how a team or an organization should conduct itself. I’ve shared my past and current experience and shared what I’d recommend some things to try if you’re interested in the field.

Meetings are inevitable; the number of how many you’re in isn’t necessarily so. Do what you can to find what works best for you, reasonably set your experiences, and then move forward.

Writing About WordPress in the Age of AI

Periodically, I review the content I’ve written over the last decade or so and am surprised to at some of the things I wrote about in the past (like My Day-to-Day). I also find it interesting that I stopped doing so. Then again, I likely exhausted that particular topic. At least for that time.

Specifically, I’m surprised that I used to write about such things despite the topics not really being relevant to what I consider my core content.

Personally, a lot has happened in the last, say, roughly five years alone – between changing jobs, growing the family moving, pursuing additional hobbies, and more – one of the things that’s taken a back seat is writing. Then again, though, isn’t that how it goes?

We have a finite number hours on how to spend our time and as that time gets allocated to other things, something gets squeezed out. And that’s what has happened with writing.

This gentleman fears the amount of time that’s passed. The perpetual ticking that surrounds him isn’t helping.

For a while, I felt guilty about it. Partially because writing daily was something that I enjoyed doing and that I did habitually. Partially because it had become such an habit that when I didn’t do it, I felt as if I was dropping the ball on something.

And though there’s truth in some of that (such as I miss writing every day), that doesn’t mean I’d trade out some of the things I’m doing that occupy that time now. Some of it’s related to my day-to-day work, some of it’s related to my family, and some of it’s related to other hobbies.


Over the holidays (and as I’m trying this, I realize I didn’t write a short Christmas post for this year which is likely the first time since I can remember not doing that), I had time to think about a lot different things some of which included both how I want to spend my time and how I currently spend my time.

Though I’m not one for setting resolutions, I’m for settings goals. And I was planning different goals for myself over the coming year, I couldn’t help but reflect a bit on this site.

Apparently, this is how Meta imagines me doing exactly what I just described. I’m drinking something out of a pepper container.

Sure, the goals would be fun to share (and maybe I will in a future post – I always enjoy what other people are planning!), I found myself thinking a little bit about software development, WordPress, the WordPress economy, where things have been, and where things are headed.

But writing about WordPress in the age of AI especially as developer is proving its own set of challenges. Of all types of people, though, shouldn’t we be here to meet it?

And with the rise of popularity in AI, the more-or-less standardization of the Block Editor, and the upcoming changes to the administration area UI, there’s a lot that can be discussed and there will likely be a lot about which to write (either via commentary or tutorials on how to achieve something).

When thinking through that, though, I found myself remembering all of things about which I used to write that weren’t always dedicated to programming but were still dedicated to what I, as a remote developer working in software developer in WordPress, was doing.

Why did I stop doing that? And what’s to stop me from doing that again?

I used to write differently about things. How did I get here?

Just as I do think tools such as ChatGPT has wrecked some of the content I (and others) have historically written, it’s by no means a call to inaction – or a call to stop writing. It’s just a call to adjust and keep moving forward.


Though I don’t know if I’ll ever write daily again, I do think there’s plenty I can share that extends beyond:

  1. Here’s what you may want to do in WordPress using PHP or JavaScript
  2. Here’s how you can do it.

So at the end of 2024, we’ll see how I’ve done. Here’s to a greater variety of content all the while still keeping the focus on the type of content about which I’ve historically written.

The Most Useful (Or Popular) Articles from 2023

Last year, I wrote the first type of article that I’ve written in a very long time (if ever) given the amount of time that I’ve been writing. The Most Useful (or Popular) Articles from 2022.

There was generally derived from analytics data but also I used light engagement metrics via X/Twitter, LinkedIn, and even email to determine what were considered the most useful (or popular) posts.

This dude is completely scared of the fact that the calendar is nearing the end of another year.

And given that we’re nearing the end of 2023, I thought I’d do the same this year. So, in keeping with the previous trend, here are the most useful (or popular) articles from 2023.


2023: Most Useful Articles

For the last few years, I’ve claimed that I want to write more than the year before and get back to how much I was writing in a few years prior. Truth is, I don’t know if this is possible given how much has changed since this. Work is different, life is different, and the way my day-to-day is structured is different.

It’s all great, but it’s different.

Regardless, I still urge everyone with a blog to continue doing the same and then syndicating out to the web. It helps surfacing your content in search engines, on social media – be it X/Twitter, LinkedIn, Facebook, whatever, – and on RSS (which is still my personal favorite).

Father Time wrapping up another blog post for the end of another year.

Anyway, these are the posts for this year. On to 2024.

Thoughts on the Upcoming WordPress Admin Redesign

During this year’s State of the Word, the new design for the WordPress administration experience was discussed.

As described by Matias:

As WordPress turns twenty years old, the overall aim of this work is to improve upon this experience at a foundational design level, giving plugins and users more control over the navigation while ensuring each WordPress experience is recognizable, intuitive, accessible, and delightful.

The full posts lays out the high-level plans for how they plan to move forward and I find the Scope section one that’s most relevant for developers (and designers who write code, as well).

This dude is completely stoked about the redesign.

It’s too long – and redundant – to post here, so I recommend reading it via the aforementioned link.


The Upcoming WordPress Admin Redesign

As a developer who’s worked in this space since “The Kubrick Era” (is that even the way some people refer to it?) and has been involved through the years of Gutenberg development, I have some thoughts relevant to the announcement that I’m sharing for posterity to see how things pan out as this project begins.

  • I was a fan of the UI refresh we received a few years ago and I’m excited about what’s planned for this one, as well. It feels like it’s going to be much bigger but it’s easier to say that when we’ve been working with the current UI for so long (and I remember the design before that – it was no small undertaking to update it, either).
  • I’m looking forward to seeing how hooks are going to be updated (and if anything new will be introduced in terms of what we’re able to do within the admin). Will be using JavaScript to work with them more? How does PHP fit into this?
  • I’m curious as to how other things have not been updated in some time (such as certain Settings screens) will be updated beyond revamping the UI. That is, part of the WordPress philosophy used to be decisions, not options but I’d say the software isn’t as decisive as it could (or should?) be. My guess is most of the options are going to stay the same, but the general look and feel is what will change.
  • I hope to see clear documentation provided for each release for developers, designers, and users to help make it clear how to achieve certain tasks. That was a challenge within the Gutenberg-era – things that worked in one release didn’t worked in a new release and the cycle was happening to fast, it was difficult to keep up.
  • I’m very interested to see how plugins will inherently interact with the new UI given many are built to perform, look, and feel native within the existing UI.
  • Performance is something that’s always a concern and if you’ve done any profiling or network monitoring within the tools the Chromium-based browsers provide, it’s evident that there’s some things that could be refactored. Is now the time? (I’m genuinely asking – this isn’t a loaded question.)
  • I’m curious to see how well the existing Block Editor will fit within the context of the new experience. Will anything need to be modified, adjusted, or tweaked to make it feel more natural or is the Block Editor, as we know it today, more-or-less standardized?

But that’s not all there is to say about it. A lot is going into the overall project especially given this is just the third phase of a larger roadmap.


Change Is Hard

As the cliché goes, change is hard. But working through change when there are so many opinions on how the change is going makes it ever harder. Further, all the training material that agencies and organizations have as well as the material they will produce are eventually going to need to be updated as the new administration area is released.

Not that kind of change. Though I guess it’s technically hard, too.

I know the ultimate goal is to have more modern administration experience that’s easier for all types of users and I’m excited to see it come to fruition.

There’s likely going to be some friction – as is the case to come with any major updates – but I’m optimistic that we’ve learned a lot from how Gutenberg was initially developed and rolled out and I hope to see others work even more closely with the administration experience to make the transition easier both for developers and users.

I Didn’t Want to Resist Gutenberg

Though I wouldn’t say I resisted Gutenberg, I definitely found plenty of times where wasn’t ready for how I wanted to use it. The learning curve was forever influx because of the how the underlying technologies were changing or how the implementation of those technologies were changing.

Dude’s as scared of this printer as some were of Gutenberg.

To put it succinctly, trying to adopt it into my day-to-day work was a challenge.

Thus, it made me slower to adopt it both as a user and as a developer until it reached a certain level. Now it’s stabilized to a point where I both enjoy writing with it and have learned enough of the APIs to build out custom blocks for my day-to-day work in a consistent way (yes, even as one of those old people who primary works with server-side technologies).

I Don’t Want to Resist the Admin Redesign

To that end, though I recognize there are going to be inevitable hurdles to cross for this update, I also think this is a time in which lessons learned over the past few years can be applied.

Maybe we’ll receive the new admin like this. This computer is hugging back with a hand of its own.

Even though it’s not a one-to-one comparison, overhauling a major component of an older piece of software once yields plenty of learning. And I hope to be more present at least in terms of making sure the things developers enjoy – such as certain actions, filters, and other things with regard to menus, settings pages, options, and so on – are easy to learn and make sense when it comes time to migrate our code.

« Older posts Newer posts »

© 2024 Tom McFarlin

Theme by Anders NorenUp ↑