On Tuesday, we talked about how to create a custom player subclass that focuses on your sport to add more functionality quickly and efficiently. Today, we’ll talk about how to create a custom game subclass.
Let’s get started.
How to add a game subclass
So before we start adding subclasses, we need to decide where this subclass will reside. You could add it to your theme’s files and then use the include function your functions.php file to hook it up. But with something like this, it’s probably best to create a custom plugin to house custom subclasses.
So, in your plugins directory, add a new directory with the name of your plugin, like sports-bench-cusotmizations. Then in that directory, create a php file with the same name as the directory, like sports-bench-customizations.php. In that file, paste the following:
<?php /* Plugin Name: Sports Bench Customizations Plugin URI: Description: A place to put customizations for the Sports Bench plugin Version: 1.0 Author: Author URI: License: GPL3 License URI: https://www.gnu.org/licenses/gpl-3.0.html */ ?>
Like the style.css file for themes, this is information for WordPress to use about your plugin. Next, go to the plugins page in you admin area of your site and activate this new plugin.
Next, create a file with the name of the subclass you’ll be creating. I’m going to be creating a soccer game, so my file will be sports-bench-soccer-game.php. Then, put the following line into the first plugin file we created so that the class file will be loaded.
include_once( plugin_dir_path( __FILE__ ) . 'sports-bench-soccer-game.php' );
Finally, to create the subclass, place the following lines into the class file.
class Sports_Bench_Soccer_Game extends Sports_Bench_Game { }
The key here is the extends Sports_Bench_Game part. This makes our new class a subclass of the normal game class in the plugin. This means that your new subclass acquires the same attributes and functions, including the class constructor, as the parent game class. This makes it super easy to customize since we don’t have to write code twice.
Now let’s start to add some functionality to this new game subclass.
Adding functions to the subclass
All right, now we’ve reached the fun part: adding attributes and functions. The attributes are simple. You can create new attributes to the class like this:
$this->game_goals; $this->game_shootout_winner;
But the real power comes with the functions you can write with the new subclass. For this tutorial, we’re going to keep it simple with two functions. One will grab a particular stat; the other will grab a number of team stats specific to soccer and return an array of them.
To create the function to grab the number of red cards for a team in a game, add the following lines to the class
https://gist.github.com/ViewFromTheBox/d268b5155584b9e54823afc3e63d4a9f
This takes one parameter, either the home or team. It then goes into the database to grab this game and then returns the number of red cards for the requested team. Easy enough.
Now let’s go one step further. Let’s now create a function that grabs stats for both teams and then output it as an array that we could use elsewhere. For this one, imagine we’ve created the other functions we’ll call in this one. Paste the following lines of code into the class.
https://gist.github.com/ViewFromTheBox/cfe6b4ba0529f53bb4890a72e7f279e1
And that’s really all we need. We can now call this function with an instance of this class and quickly get a team stats array, all without using SQL. And this is just the tip of the iceberg.
What does this mean for your site?
So what does this all mean for you? Well, it means that you can make customizing your site to fit you needs a whole lot easier. The reason the game and other classes were created in the first place was to minimize the need to write more SQL to get data from the database. Now you can do the same with your customizations.
What you do with this new information is up to you. Let your imagination run wild. And if you come up with a cool implementation of a game subclass, let us know. We’ll feature it on the site.
Happy developing!
[sports-bench-other-posts category_slug=”tutorial”]
Leave a Reply