Software Engineering in WordPress, PHP, and Backend Development

Author: Tom (Page 72 of 427)

The Why and How of Custom jQuery Events

As far back as 1.0, jQuery has provided a trigger function that allows us to:

Execute all handlers and behaviors attached to the matched elements for the given event type.

But it wasn’t until 1.3 that this particular function became significantly more useful, at least as far as I’m concerned. I say that because that’s when we gained the ability to define custom events and then set handlers for them.

Custom jQuery Events

What, though, is a likely use case in which custom jQuery events are useful in the context of WordPress? Off the top of your head, you may be able to come up with many. Or maybe not.

It hasn’t been until lately that I’ve been using them significantly more than usual. So I thought I’d share how I’m using them if for now other reason than showing you how to wire them up to your work.

Continue reading

Aim to Break Programs into Smaller Components

One of the things that programmers often talk about is the desire to break programs into smaller components, or functions, so that it makes them easier to trace, easier to read, and easier to debug.

But it’s not all that uncommon to see monolithic functions with a lot of code comments to help explain what’s going on in the program.

Breaking Things Into Smaller Components: Monoliths

A monolith ala 2001: A Space Odyssey.

I’m not knocking this, really, because I don’t know the constraints under which a programmer was working. That is:

  • What was the budget s/he had when building the program?
  • How much time was given to complete the project?
  • Were there many people working on the project?
  • Was the programmer given time to write the code so they could unit test it, refactor it, or merely make it easier to read?

In short, there’s a lot of reason – I believe – that we can read “bad code,” and it doesn’t always have to be the fault of the programmer (that’s just the most natural thing we have to throw out when we read something we dislike).

Does this mean, though, that we shouldn’t strive to refactor or write code in such a way that makes it easier to understand? Of course not. Assuming we have the time to do so, how might we do it?

Continue reading

Writing Messages to the WordPress Debug Log

Whenever I’m working on a project, I often have WordPress set in debug mode, and I like to write messages to the error log that I can easily view, trace, and follow whenever I’m working on a project.

WordPress Debug Log: Monolog

When I do this, there are two ways (and it just depends on the project):

  • I’ll use a library such as Monolog,
  • I’ll use my own, simple log function.

In this post, I’m going to cover the latter. That is, I’m going to share how I write messages to the WordPress debug log and then some of the things you may need to pay attention to whenever you’re doing the same.

Continue reading

A Stub for Working with Database Queries in WordPress

As much as I am a fan of certain WordPress APIs for querying the database such as:

I’m also a fan of querying directly against the database when needed.

As you’ll notice, there’s a consistent way that I go about doing this and since there are times where I’ve been doing this more frequently, as of late, here is a basic format for working with database queries in WordPress.

If I do this, though, there are generally do things that I keep in mind:

  1. always make sure the query is parameterized (especially in the case of user input),
  2. return in the information in the form of an associative array.

The first case is for security; the second is more or less for convenience to make iterating through the data simpler via PHP array functions.

Continue reading

An Easy Way to Cast a PHP Standard Class to a Specific Type

If you work with object-oriented PHP in WordPress and you’re building out various models that fit your web applications, the odds are that you’re going to deal with retrieving serialized versions of those models at some point during a program’s execution.

Here’s the thing, though: Sometimes, that unserialized data come back as standard PHP classes. This means that if you inspect the type (through various debugging tools), you’re going to see they are the type of stdClass.

Cast a PHP a Standard Class to a Specific Type: Type Juggling

If you’ve been properly building your models those, your code is going to have functions that the stdClass does not, and you’re going to want to call on them.

Further, you can’t simply cast them from one type to another like you can with native types (such as strings, integers, and so on). In situations like that, you need to be able to cast a PHP standard class to a specific type.

And here’s a function that will help you do just that.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑