How To

It’s Just Math

Understanding a little more about hooks explains how I run simple math on post saves.

Recently I posted about how I added new features to a show scoring system, over on LezWatchTV. We added in what we call Intersectionality, which is rewarding shows for positive representation of diversity in the world. Naturally that begs the question of what is show scoring (answer: it’s calculative a qualitative value of how good a TV show on a scale of 0-100). And that makes people ask …

How do you do that?

It’s really math It’s really math

Look, the bare answer to all of this is that it’s math. I’m taking the meta data we assign to shows, like how much screen time characters get and how good the show is for them, and so on, and assigning numbers to those values. Then I add up the numbers, in various ways, and determine what the over all score is.

It’s a little more complex than that, and if you’re super interested in how it all works, we wrote up an explanation as to the framework I created to value shows.

But most people who ask me how I did this aren’t asking about the math, they want to know about the code. That is, how did I get WordPress to automate all this, because you know I don’t do it all by hand. No, I do it when the post saves.

Top ↑

Magical Hooks Magical Hooks

Let’s take a moment. What’s a hook anyway?

A hook is an action or a filter in WordPress, which allows you to write code that ‘hooks’ into the rest of WordPress code. You can use this to trigger your code to run at specific times.

For example, if I had a hook called publish_post (which we do – WordPress has that built in), and I wanted to run my show calculations when I publish a post, I would do this:

add_action ( 'publish_post', 'shows_calculations' );

That would of course require me to have a function called show_calculations() that does the math, which is fine. But. I don’t want to do this on post publish. I want to do it on post save, and only if the post is of the shows post type. And that sounds like a lot of if/then statements until you learn about dynamic hooks.

You see, I’m using the save_post_ hook, which runs when a post is saved, and it’s a little special. Unlike a hook like publish_post(), this hook lets me customize it how I want by being save_post_{$post->post_type} …

Yeah those curly brackets are weird, right? That’s the dynamic part. That part changes.

Top ↑

Dynamic Saving Dynamic Saving

To understand how the hook name changes, you should know that in my case, I have a post type called post_type_shows — logical right? Well the subsequent hook is called save_post_post_type_shows — that is, I’ve replaced those weird brackets with the post type.

Once you know your new hook name, it’s like every other hook:

add_action( 'save_post_post_type_shows', 'shows_calculations', 10, 3 );

What that does is call the function post_type_shows_calculations on every post save. And that function calls another one I called do_the_math() which passes the post ID to the myriad complications of calculations I perform.

But the magic literally is in that post save hook.

1 reply on “It’s Just Math”

Diversity in television and media is extremely important. I’m so glad to see that you’ve developed a scoring system to hold shows accountable for showing proper diversity in their program, and I would love to see it implemented in other media outlets as well. With how important and often used electronic media is currently, those outlets should be held accountable for showing diversity as well. Working with a 0-100 scale for voting makes perfect sense to me!

Comments are closed.