At this point, we have a semi-functioning WordPress plugin that could do something if we created a “customer” storefront. But without being able to update (activate and deactivate games) or delete them, this thing would be very limited in its functionality. Let’s move forward and be able to make some changes to our plugin.
As noted before, we will not be covering the general aspects of how to write PHP code or work with MySQL. To learn MySQL, you can get tutorials online and in printed form. To learn PHP, you can find resources online and in printed form here. Great free tools to use to write code (known as “Integrated Development Environments” or IDEs) are Komodo (for OS X) and NetBeans (for Windows).
Deleting and Activating The Data
Difficulty: Easy
Completion Time: 1-2 hour
What We Will Be Using: PHP, WordPress Platform, MySQL
Creating The Links To Activating Games
Yesterday, we looked at using the POST method for adding a game to the library which is perfect for using that form, but the GET method is going to what we use to activate/deactivate games and delete them. Let’s look at the code to build a link to activate or deactivate a game.
if($game_status == 0 || $game_status == 1){ // Status of the game is currently pending or inactive echo "activate"; } if($game_status == 2){ // Status of the game is currently active echo"deactivate"; }
There are three things to note in this logic: we use the status of the game to decide if we need to activate or deactivate the game, we are using the “action=activate” to define what information the page will be receiving, and the game_id which will define which game we are activating or deactivating. These links will be posted and in the games_options() when we display all the games, giving the administrator full control over what the user experiences.
Updating The Database
To actually update the database, we will need to collect the information from the URL by posting the following code before you display the data, outside of the foreach loop and update the database:
if($_GET["action"] == "activate"){
$sql = "UPDATE " . $table_name . " SET `status` = 2 WHERE id = " . $_GET["id"];
$results = $wpdb->query($sql);
echo "
Game Activated.
“;
}
if($_GET[“action”] == “deactivate”){
$sql = “UPDATE ” . $table_name . ” SET `status` = 0 WHERE id = ” . $_GET[“id”];
$results = $wpdb->query($sql);
echo ”Game Deactivated.
“;
}
This IF statement is situational and simply updates the status of the game depending on what the action needs.
Implementing The Delete Function
The delete action is very similar to the activate/deactivate action, except there is no IF statement. Here is the link to delete a game, it should be placed right after the activate/deactivate link:
echo " - delete
“;
Note that we did not close the cell in the activate/deactivate IF statement because this includes the delete link afterwards. Now for receiving the code:
if($_GET["action"] == "delete"){
$sql = "DELETE FROM " . $table_name . " WHERE id = " . $_GET["id"];
$results = $wpdb->query($sql);
echo "
Game Deleted.
“;
}
Super simple code that goes after the activate/deactivate retrieving code. And that is it! We are one more step closer to a complete plugin. A few more steps and we will have a fully functioning administration side for our plugin.
Speak your mind...