The last two days we have simply been doing the underlying planning for this project and creating the database for this plugin, but now it is time to get into the coding and the entire WordPress Codex. In today’s post, we will be creating the initial file and ensuring that we are displaying the information in the database correctly.
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).
Displaying The Data
Difficulty: Easy
Completion Time: 1-2 hour
What We Will Be Using: PHP, WordPress Platform, MySQL
Creating the Main Plugin File
Before we get into coding, we need to create our main plugin file with a unique PHP filename in a folder that will be the name of our plugin. For our project, we named the file game_plugin.php and the folder it goes into wp_game_plugin. You can really name it whatever you want, it doesn’t make any difference, but make sure that it is memorable and appropriate to your project. Open your blank main file and at the top, put this header information with your own data:
Now upload this whole folder to “../location/of/your/wordpress/wp-content/plugins“, open your WordPress administration panel in your browser, navigate to the Plugins sections, and you should see your plugin in the list.
Getting the Data From The Database
Now we will get data from the database. Before we do this, open up your MySQL database manager and manually add some correct information that you might see in the database that we can then pull from it. Once you have done this, open up your main plugin file again.
We will be putting in code to give our plugin a spot on the left side administration menu of WordPress. To understand what this means, you can look up the details of the different files for add_action, add_menu_page, and add_submenu_page to get deeper details of each. Add the following code and change the names to what you want (make sure you change it exactly the same everywhere):
add_action('admin_menu', 'games', 10, 2 );
function games() {
add_menu_page('Games LIst', 'Games List', 'administrator', 'games', 'games_options');add_submenu_page( 'games', 'Games List', 'Games List', 'administrator', 'games', 'games_options');
add_submenu_page( 'games', 'Add Game', 'Add Games', 'administrator', 'add_game', 'add_game' );
}function games_options() {
}
Re-upload this file to the plugins folder on your server, log in to WordPress and look for your plugin in the Administration Menu.
Formatting Your Data Correctly
Now that our page is displaying in the administration page, lets go ahead and grab some information. You can get a more detailed list of $wpdb if you feel lost. In your main plugin file, add the following code into your games_options function:
global $wpdb;
$table_name = $wpdb->prefix . "games"; // Change the quotations to what your MySQL database name is$myrows = $wpdb->get_results( "SELECT * FROM " . $table_name );
foreach ($myrows as $myrows) {
echo $myrows->id . ", " . $myrows->title . ", " . $myrows->description . ", " . $myrows->materials . ", " . $myrows->status;
// Change these items to the columns in your database
}
It is that easy! You now have some information pulled from your database and coming up in your plugin in WordPress that can be accessed via a custom slot in the Administration Menu.
Speak your mind...