How to use PHP array functions instead of loops

I gave a talk at WordCamp Los Angeles 2016 on PHP array functions. This is the companion article that I wrote for it. If you’re just looking for the slides, click here.

As a WordPress or PHP developer, you use arrays all the time. They’re an essential (if not necessary) part of your developer toolbox. But that doesn’t mean that you’re using them to their full potential.

That’s because, when we work with arrays, we also tend to work with loops as well. And loops are seductive. They let you traverse an array and perform any operation that you wish on each array element.

That said, it’s easy to overuse loops. When that happens, your code becomes hard to read and to test. That’s because loops, while easy to use, can also make your code much more complex.

But, lucky for us, PHP has a wealth of array functions. They’re used by PHP experts to make their life easier and replace a lot of instances where you’d use a loop. This, in turn, makes their code simpler, easier to read and more testable.

Continue reading How to use PHP array functions instead of loops

Designing a class to build simple WordPress queries

In a previous article, we looked at how you could create a class to manage WordPress posts. We ended creating a class who’s job it was to interact with the WordPress database. And it did that quite well!

As part of its job, our class also had to be able to query the WordPress database. To do that, we made it easier to reuse code around the WP_Query class. We achieved that by creating methods that used predefined query arguments.

But have you ever looked at the codex page for WP_Query? Holy Moley, there are a lot of query parameters in there! It’s pretty intimidating and not always easy to use in practice.

That’s the problem that we’re going to look at in this article. We’ll design a class to simplify how we build WP_Query objects. It’ll handle all the complexity around WP_Query query parameters for you. The result should be an easier way for you for you to create WordPress queries.

Continue reading Designing a class to build simple WordPress queries

Thoughts on teaching object-oriented programming with WordPress and overengineering

This site has grown a lot in the last year or so. We’ve spent a lot of time going over object-oriented solutions to WordPress problems. So much so that, we have a small library’s worth of material that you can refer to.

With any type of growth, criticism will arise. This is a good thing. I’m a big proponent of “Strong opinions, weakly held“. But this can’t happen without discussion and debate. In the long run, I always feel it leads to a better outcome. (Well, that’s as long you don’t get stuck just debating!)

Recently, there was a debate on the nature of the solutions that you’ll see on this site. They’re overengineered. I think this is a valid criticism and I wanted to share some of my thoughts on the whole thing.

Continue reading Thoughts on teaching object-oriented programming with WordPress and overengineering

Designing a class to assemble plugin classes

Learning object-oriented programming with WordPress is a lot like embarking on an epic journey. You’ll experience some lows, some highs and some downright frustrating moments. But it’s all part of the learning process.

And, so far, we’ve done quite a bit of progress on our journey. We’ve looked at various WordPress problems. And we then solved them using a class or combination of classes.

We call this process “object-oriented design“. It’s been the focus of a lot of the articles on this site. Because of that, there’s a good chance that you’ve begun using it in your own projects. (And if you haven’t, you should try it!)

When that happens, you’ll start to notice that your project has a lot of classes. This tends to become a problem for a lot of developers. They wonder, “How do I assemble all these classes together!?” It’s not unusual for them to revert back to the standard WordPress way of doing things when that happens.

This is the problem that we’re going to explore in this article. We’ll look at how you can solve it using a dedicated class for it. To do this, we’ll start thinking about the job of the main plugin class.

Continue reading Designing a class to assemble plugin classes

PHP strings and how to format them

Strings have always been an integral part of the programming world. We use them so much because they do something that’s almost impossible to do with other data types. They allow our code to communicate with others.

The definition of “others” is quite wide in this context. Others can be as straightforward as someone using your WordPress plugin. But it can also be more abstract like a browser reading HTML code generated by PHP. They are both a third party who interacts with strings generated by your code.

Sometimes, these strings are simple and used as is. But more often than not, these strings need to be dynamic. They need to adapt to what’s going on inside your code.

This latter one is why it’s important to have a good understanding of what you can do with strings in PHP. That’s because there are countless ways that you might want to alter a string. For example, you could have a string change based on certain conditions. Or you could insert one or more variables into it.

For this article, we’re going to focus on how you can format a string. There are a lot of built-in helper functions for strings in PHP. We’ll look at the ones that can help you achieve that goal.

Continue reading PHP strings and how to format them

A look at the modern WordPress server stack

I gave a talk at WordCamp San Diego 2016 on the modern WordPress server stack. This is the companion article that I wrote for it. If you’re just looking for the slides, click here. It was also later republished on Smashing Magazine.

Do you remember when you could run a “fast” WordPress site with just an Apache server and PHP? Yeah, those were days! Things were a lot less complicated back then.

Now, everything has to load lightning fast! Visitors don’t have the same expectations about loading times as they used to. A slow site can have serious implications for you or your client.

As a consequence, the WordPress server stack has had to evolve over the years to keep up with this need for speed. As part of this evolution, it’s had to add a few gears to its engine. Some of the older gears have had to change as well.

The result is that today the WordPress server stack looks quite different from a few years ago. To better understand it, we’re going to explore this new stack in detail. You’ll see how the various pieces fit together to make your WordPress site fast.

Continue reading A look at the modern WordPress server stack

WordPress for the adventurous: WP_Query class

It’s not too far-fetched to see WordPress as a library. You write a post and publish it. Meanwhile, WordPress classifies it and puts it away on its shelves.

But how do you find your post again after WordPress shelved it away? You need someone to help you navigate this huge library and find what you’re looking for. You need a librarian!

That’s the job of the WP_Query class in a nutshell. It’s the librarian of the WordPress database. You talk to it when you want to search through the WordPress database. It’ll help you find the information that you need!

It does this without requiring that you know how WordPress stores that information. That means that you don’t have to know how to write MySQL queries. WP_Query takes care of all that for you. (Yay!) It’ll transform your search request into a safe MySQL query and process it for you.

But how does the WP_Query class do this? That’s what we’re going to look at today. We’re going to explore how it works and what it can do for you. This will help you leverage it to the maximum in the future.

Continue reading WordPress for the adventurous: WP_Query class

The importance of naming in programming

There are only two hard things in Computer Science: cache invalidation and naming things.

This is a quote attributed to Phil Karlton, a famous Netscape (now, that’s a trip to the past!) developer. It’s, without question, my favorite programming quote. I share it whenever I can (like now!).

On the surface, it seems like only one of those things is hard. That’s cache invalidation. It’s the process of removing invalid data from a cache. If you’ve ever built anything with WordPress object cache, you know that it’s not easy to do at all.

But naming things? Who struggles with that? That seems like the easiest thing in the world. And that’s true in a way. Naming something doesn’t take a lot of effort.

Here, I’ll even name three variables for you! They’re pirate, wizard and ninja. You might wonder, “Why did he pick pirate, wizard and ninja as variable names?” No reason. They just sound awesome!

The point of using a silly example like that is to show you that you can name variables anything that you want. It’s not hard to find a name. The hard part is picking a good name. It needs to be clear and mean the same thing to everyone reading.

So what can you do to ensure that you’re naming things as best as you can?

Continue reading The importance of naming in programming

Teach everything you know

For the last year and a half, I’ve been teaching everything I know to the WordPress community. This hasn’t been easy to do and it’s been quite a journey so far! That’s why I wanted to share my current beginners experiences.

My hope is that it might inspire others to also start teaching themselves. It’s a great way to improve your career while strengthening the WordPress community. WordCamp Miami organizers also shared my enthusiasm for sharing this.

They invited me to speak about it to BuddyPress developers at WordCamp Miami 2016. This is the companion article that I wrote for it. If you’re just looking for the slides, just click here.

Take a moment to think about who you trust online. What do they have in common? There’s a good chance that they thought you something valuable.

Some of the biggest names in the WordPress community share their knowledge on a regular basis. They write on their blogs, speak at WordCamps and so on. That’s how you remember them and it’s also why you trust them. That trust that you have in them can translate to different things.

The most common outcome is that you might buy a product that they sell. That’s because they educated you about the problem it solves. Now that you know more about it, you want a solution for it.

This also applies to open source projects. They taught you about the problem that their project solves. But, in the process, they also demonstrated their technical ability to solve it. The result is that you might decide to use their project over someone else’s.

You might want to hire them to work on a project or for your company. Through their teaching, you have a better idea of what they can bring to you and your team. Their teaching material also gives you an opportunity to look at the quality of their work. In the end, you’re not trying to hire an unknown quantity.

These are just some of the benefits that come from teaching you something valuable. And the good news is that you can do that too! You have valuable insights that you can share with others.

But teaching isn’t without its own set of obstacles. Today, we’re going to focus on the ones that you’ll face as you begin your teaching journey. We’ll look at what they are and how you can overcome them.

Continue reading Teach everything you know

Design a system: WordPress event management

In a previous article, we went over the concept of events and event listeners. These were classes who were in charge of a specific aspect of an application. This was a more abstract job that touched several parts of an application.

We also saw how you could design these event listeners in WordPress. It required that you rethink how you use the plugin API. (Yes, this is going to be another plugin API article :P)

We’re going to keep going with this idea of events and event listeners. We’re going to design a system for them. We’ll call it an “event management” system. It’s an important tool in your journey to master object-oriented programming in WordPress.

Continue reading Design a system: WordPress event management