Software Engineering in WordPress, PHP, and Backend Development

Author: Tom (Page 281 of 428)

How to Sort an Array by Locale in PHP

As easy as it is to translate content within WordPress, there are  times in which some of the content in the database may be different than the original locale of the site.

Say, for example, you’re running a site that includes a list of names in a flavor German when your server (or even perhaps your site) is based on an English locale. The goal is to read the names into a collection – rather, an array – and then sort them alphabetically.

Obviously, different locales have different alphabets so what works in, say, the United States won’t necessarily work in Germany.

There are a couple of ways to go about handling this, but the easiest way that I’ve found is to use PHP’s Collator class.

Continue reading

Passing Variables in WordPress: Loops

Check out the first post on passing variables in WordPress using WP_Query.

In the previous post, I shared how I used to use the following rule of them when passing variables in WordPress (and other programming languages, really):

“Only if I need to use this value more than once, then I will store it in a variable.”

And though I still think it’s a fair rule to follow, there are exceptions.

The more time I’ve spent writing code and having to maintain codebases from both myself and others, the more I’ve appreciated having variables defined – even if they are used only once – that hold more complex information for the sake of readability, clarity, and maintainability.

In the last post, I looked at an example as it relates to `WP_Query` since it’s one of the more powerful aspects of the WordPress API, but I’ve found that the same is true in simpler complex such as loops.

Continue reading

Passing Variables in WordPress: WP_Query

Check out the second post on passing variables in WordPress in loops.

One of the most minute aspects of writing code is opting when – or when not – to use variables.

It sounds like a trivial decision, right? And in some respects, it is, but the longer I’ve been writing code (or, perhaps a better way to put it), is that the more code that I’ve written and had to maintain, the more my opinion has changed on how frequently I use variables.

After being in the industry for a couple of years, I tried to follow the this rule of thumb:

“Only if I need to use this value more than once, then I will store it in a variable.”

And I still think that’s a decent rule of thumb, but the more I’ve worked with systems that, say, interface with a database, or that leverage multi-dimensional arrays, or that some type of iteration (be it an each iterator, a foreach iterator, or something similar), the more I’ve found how storing a variable – even if it’s only going to be used once – can make for slightly more readable code.

Continue reading

Coding Standards and Guidelines (“When in Rome, Program Like the Romans”)

Recently, I was talking with a fellow developer about the various strategies there are for writing code.

By that, I mean we’ve got a number of different paradigms such as object-oriented programming, functional programming, and procedural programming. We’ve got design patterns, strategies, ideas like SOLID, domain-driven design, aspect-oriented programming, and so on.

When it comes to these things – and as with most things – developers can be really opinionated (myself included) about which things are the best to use. To some degree, I think that there’s some part of us that wants to believe the thing that we’ve just learned is “the next great thing” as it relates to writing code.

Maybe that’s an amateur’s mistake, though. I mean, that’s how I felt earlier in my career – I remember whenever I learned, say, domain-driven design and all of the stuff that comes with it, I wanted to implement in all of my future projects because I thought it makes the code so much more readable.

But over time, my opinions have changed.

Sure, I still really like certain aspects of all of the aforementioned programming techniques, but I also know that I can’t apply them all in what I’m doing.

Continue reading

Organize WordPress Terms in Alphabetical Order

I’d argue that, at this point in time, working with WordPress taxonomies has never been easier.

Sure, creating, tagging or categorized, and adding custom taxonomies to posts and custom post types has always been relatively easy, but when it comes to implementing a solution that utilizes custom post types and/or custom taxonomies, then constructing queries can sometimes be a bit of a pain.

But now, we have more API functions and features than ever before:

Of course, there are still cases in which retrieving and organizing data still leaves something to be desired. 

I don’t fault WordPress for this, though. It’s more of a function of how data is ordered within databases, applications, and in computers in general.

Continue reading

« Older posts Newer posts »

© 2025 Tom McFarlin

Theme by Anders NorenUp ↑