Press "Enter" to skip to content

Tag: PHP

10 Ways to Learn WordPress Hooks

WordPress Hooks are hard. We always hear the word “hooks” being thrown around and it’s one of those things that make sense, but then it doesn’t.

I’m sure that there may even people reading this right now, and not entirely sure what I mean by hooks. That’s okay. Before I go on with this post, they look like this. These are two examples I pulled from the Twenty Fifteen Theme in functions.php.

Line 142 of functions.php in Twenty Fifteen.
Line 142 of functions.php in Twenty Fifteen, it’s an action hook.
Line 310 of functions.php in Twenty Fifteen.
Line 310 of functions.php in Twenty Fifteen, it’s a filter hook.

 

Still confused? No worries, it took me a while too. In fact, I can’t say that I completely know all there is to know about WordPress hooks even now, even after writing, “The WordPress Hooks Firing Sequence“. WordPress is the kind of thing that the goal isn’t to learn it all, but to learn it in levels for your purposes. It’s such a deep system, deeper than most people imagine, and I’ve found that once I’ve obtained an understanding of one concept, there is always more to learn. Each concept I learn serves as a foundation for what I learn next.

Hooks didn’t click for me until about a year ago. I too, was copying and pasting various solutions offered by the all knowing Google or Stack Overflow into my WordPress theme. I hadn’t truly understood what was happening when I dropped these snippets of PHP in and the world that would open once that understanding happened.

As much as I’d like to sit here and try to explain what hooks are, if there’s one thing I can say from experience, is that understanding comes in layers. In other words, there is no one mind blowing resource that is suddenly going to make it click. Or maybe it will, depends on the person I guess. For me, however, it took several readings, videos, and diving into WordPress code before the ah hah moment hit.

Knowing that it took a combination of resources and methods for me to learn WordPress hooks, I’d like to pass them onto you. These resources will hopefully bring that fuzzy definition of hooks into focus, even if it’s in layers. In addition to these 10 resources, I’d like to share 2 methods that worked for me. Everyone learns differently and at their own pace, but perhaps sharing my experience will help someone along.

2 Comments

How to Add Counter Classes with a Global Variable

Ever wonder how to add counter/numbered classes to your posts? Is this the fourth post or the seventh? Well, if you have wished upon a star (or even if you didn’t, but it was more like cursing out your computer, that’s fine too), then your wish is about to come true with this tutorial.

In this tutorial’s example, we want our blog posts to have classes like “article-number”, and we can achieve this using the post_class(); function and a counter that we’ll set as a global variable.

It’s not as hard as you think. Also, keep in mind that this is a technique that can be applied to other scenarios too, and it’s not limited to just adding classes to our blog posts. Well, let’s get started, shall we?

Before we begin:

  1. Some familiarity with loops and general PHP will help.
  2. Might want to review what variables are. I also go over variables with better analogies in an old article of mine on the Pixafy Blog: Understanding WordPress’ $args Variable
  3. Make sure you have a theme that you can edit files in – for my example, I’m using TwentyThirteen. The files I’ll be working in are: functions.php, index.php, and content.php.

What we’re going to achieve:

global-results

Leave a Comment

How to Apply a Template to Sub-categories

The big question: Does category.php apply to the children of the targeted category?

The answer: No. So now what?

For those not familiar with the power of category.php within a theme, you can catch up here. In a nutshell, you can apply a custom template to a specific category just by naming the template file with a convention that gets automatically picked up by WordPress. It’s a really awesome feature to take advantage of, but there’s one problem.

Category.php will not apply to the category’s children/sub-categories.  So then, how do we apply a template to sub-categories? After lurking around online to find forum threads like this, and brainstorming with a co-worker of mine, I found some solutions to the problem and wanted to share them.

Before we begin:

  • You should have some knowledge as to how category.php works in a theme
  • You should have some basic understanding of PHP or at the very least, have taken a look at WordPress’ Conditional Tags
  • You should have access to your theme’s files for editing purposes
1 Comment