WordPress Plugin Construction for the Non-Programmer: Part VI

WordPress Plugin Construction for the Non-Programmer: Part VI

Welcome to the sixth post in my series on building a WordPress plugin from scratch. This tutorial series requires no programming experience, but I do suggest you start from the first post in this series if you’re a total newbie to WordPress development.

Just a couple things to keep in mind about this series if you’re just tuning in:

Five Official “Cover my Butt” Statements

  • At the time of this writing, the most up to date WordPress version is 3.4.1. It might be different by the time you read this.
  • I’m assuming you already know how to install a basic WordPress site, if you’re unsure please see their www.wordpress.org.
  • Please do NOT attempt anything in these tutorials on one of your live WordPress sites. Rather, set up a subdomain and a brand new WordPress installation to use for plugin testing purposes only.
  • If you’re confused by any of the code or terminology above, please go back and read the first five posts in this series.

Basic WordPress Plugin Source Code for This Tutorial

Below is the source code for this tutorial. I’ve provided this for example purposes but strongly encourage you to recode this yourself from scratch and to experiment with some minor modifications:

{code type=php}

<?php

/*/

Plugin Name: Basic

Plugin URI: www.yourwebsiteurl.com

Description: Demonstrates how a WP plugin works and how to create a unique class for your WordPress plugin.

Version: 101

Author: Your Name

Author URI:  www.yourwebsiteurl.com

/*/

// check for existing class //

if(!class_exists(“YourBrandWPBasic”)) {

// create class //

class YourBrandWPBasic {

//plugin constructor//

function YourBrandWPConstructor(){

}//close constructor //

//create admin page//

function YourBrandWPAdminPage() {

//lock out unauthorized users//

if (!current_user_can(‘manage_options’)) {

wp_die( __( ‘ KEEP OUT! Authorized Personnel Only!’ ) );

}//end lock out/

//spit out html for admin area//

echo ‘<div>’;

echo ‘<h1>Welcome to Your Brand WP Basic</h1>’;

echo ‘<p>This will be the command center for the Basic Plugin customization features.</p>’;

echo ‘</div>’;

//grab users information from the database//

global $wpdb;

//grab user data//

$user_data = $wpdb->get_row(“SELECT * FROM $wpdb->users WHERE ID = 1”, ARRAY_A);

echo “Username: “. $user_data[‘user_login’];

echo ” | User Email: “. $user_data[‘user_email’];

echo “<br />”;

echo “First post by “. $user_data[‘user_login’] . “: “;

//grab user post data//

$user_post_data = $wpdb->get_row(“SELECT * FROM $wpdb->posts WHERE post_author = ‘” . $user_data[‘ID’] . “‘”, ARRAY_A);

echo “<br />”;

echo $user_post_data[‘post_title’];

echo “<br />”;

} //end of html for admin page//

//build plugin menu//

function my_plugin_menu() {

add_options_page(‘Your Brand WP Basic’, ‘Your Brand WP Basic’, ‘manage_options’, ‘your-brand-wp-basic-admin’, array(&$this, ‘YourBrandWPAdminPage’));

}//end plugin menu

} // close class //

} //close check or existing class //

//if the class exists, assign a handler to it//

if(class_exists(YourBrandWPBasic)){

$your_brand_wp_basic = new YourBrandWPBasic();

//tell your handler to build the plugin menu using WP action

add_action( ‘admin_menu’, array($your_brand_wp_basic, ‘my_plugin_menu’) );

}//close assignment of handler//

{/code}

BTW, a quick disclaimer about these source code snippets based on some feedback from one of my more experienced readers…

The “//” which you see after each of my comments is not considered standard practice for PHP commenting. Comments in PHP normally start with either “/” or “//” but don’t end in “/” or “//” as they do in my examples. The code will function the same whether you comment as I have or follow the conventions, just be aware that I’m deviating from the standard a bit.

And now, let’s break down our new lines of code in plain English.

Step #1: Use the $wpdb Class to Talk to the WordPress Database

In the fourth post of this series, I showed you how to create a basic menu for your plugin within the WordPress admin panel as well as a very simple admin page. In this tutorial, we’re simply using the $wpdb class (covered in the fifth tutorial of this series) to display some information from the WordPress database in the admin page of your plugin.

This will be accomplished through the portion of code which we added to the method called “YourBrandWPAdminPage” which is a “tool” in your “YourBrandWPBasic” class (toolbox).

The code for the entire “YourBrandWPAdminPage” method is below:

{code type=php}

function YourBrandWPAdminPage() {

//lock out unauthorized users//

if (!current_user_can(‘manage_options’)) {

wp_die( __( ‘ KEEP OUT! Authorized Personnel Only!’ ) );

}//end lock out//

//spit out html for admin area//

echo ‘<div>’;

echo ‘<h1>Welcome to Your Brand WP Basic</h1>’;

echo ‘<p>This will be the command center for the Basic Plugin customization features.</p>’;

echo ‘</div>’;

//grab users information from the database//

global $wpdb;

//grab user data//

$user_data = $wpdb->get_row(“SELECT * FROM $wpdb->users WHERE ID = 1”, ARRAY_A);

echo “Username: “. $user_data[‘user_login’];

echo ” | User Email: “. $user_data[‘user_email’];

echo “<br />”;

echo “First post by “. $user_data[‘user_login’] . “: “;

//grab user post data//

$user_post_data = $wpdb->get_row(“SELECT * FROM $wpdb->posts WHERE post_author = ‘” . $user_data[‘ID’] . “‘”, ARRAY_A);

echo “<br />”;

echo $user_post_data[‘post_title’];

echo “<br />”;

} //end of html for admin page//

{/code}

Everything above the “//grab users information from the database//” comment should look familiar, we’ve covered it in the past few tutorials of this series.

However, you’ll notice that beneath that comment we’ve added some new codes. First, we set the global variable (global variables were explained in post V of this series) $wpdb to show that we’ll be using the WordPress $wpdb class to “talk” to the WordPress database:

{code type=php}

global $wpdb;

{/code}

In the next line of code, we apply the $wpdb class to grab some data from the table called “users.” Let’s break down look at exactly what this code is doing:

{code type=php}

$user_data = $wpdb->get_row(“SELECT * FROM $wpdb->users WHERE ID = 1”, ARRAY_A);

{/code}

First, we’re storing everything to the right of the “=” sign in a variable called “$user_data.” The commands to the right of the “=” then tell us exactly what information will end up stored in the “$user_data” variable.

First, I’m using the “$wpdb->get_row” to tell WordPress that I’m calling a method named “get_row” which is a part of the “$wpdb” class. The method “get_row” is another native WordPress function. It simply means that I’m grabbing a row of data out of the WordPress database. Between the “(“ and the “)” are the two pieces of information which the “get_row” method is operating on, separated by a comma.

The first piece of information is:

“SELECT * FROM $wpdb->users WHERE ID = 1”

In the previous post of this series, I explained that this command ^ is telling WordPress to “SELECT” * (“*” stands for “everything) “FROM” the WordPress database ($wpdb) table named “users” “WHERE” the “ID” column is equal to the number one.

Pretty simple right?

Just one thing you need to be aware of…the actual table within the WordPress database is called “wp_users,” not “users.” So why are we saying “$wpdb->users” instead of $wpdb->wp_users?” Because that’s the way the WordPress developers decided to build the “$wpdb” class, so keep that in mind while you’re communicating with the WordPress database using the “$wpdb” class.

The next piece of information is “ARRAY_A” which is another WordPress command. Remember that an array is a string of information, and it can contain letters and words only, numbers only OR it can contain both letters and numbers. When an array contains letters and numbers, it’s called an associative array.

If you look at the “wp_users” table of your WordPress installation, you’ll notice that the rows of data in that table contain numbers and letters as well as words. So can you guess why we’re using “ARRAY_A?” We’re using it because that “A” stands for “associative.”

There are other options available depending on what kind of data you’re retrieving, but just between you and I, you can use “ARRAY_A” in just about every case. I do it all the time. So now we’ve got our data out of the WordPress database, time to do something with it…

Step #2: Display the Data Selected from the WordPress Database

Once we’ve scooped our data out of the WordPress database, we simply apply the rules you’ve already learned in this series to spit out some of the data contained within the “$user_data” variable:

{code type=php}

echo “Username: “. $user_data[‘user_login’];

echo ” | User Email: “. $user_data[‘user_email’];

echo “<br />”;

echo “First post by “. $user_data[‘user_login’] . “: “;

{/code}

We’ve already talked about what the echo command does in the first tutorial of this series. But I’ve included a few new tricks in the code above. First, I used the double quotes to echo out a piece of literal text which reads:

Username:

Next, I used the double quotes to show that I was closing out of plain text and moving into a PHP variable called “$user_data[‘user_login’].” Then, I used the semi-colon to close out of that PHP line. That’s how you move between echoing PHP variables and echoing plain text or html.

However, notice that on each of the four lines of echoed code, there’s a period between all of PHP variables and the regular text. This is called “concatenation,” it basically means that I’m “gluing” pieces of information together to create a string of information.

The period is simply the glue which attaches PHP variables to regular html (or text) or which glues one PHP variable to another. For example, if I had wanted to echo out the variables “$user_data[‘user_login’]” and  “$user_data[‘user_email’]” I would have needed to glue (concatenate) them together using the period, like this:

{code type=php}

echo $user_data[‘user_login’]  . $user_data[‘user_email’];

{/code}

So assuming that my user login was “logindude” and my email was “[email protected]”, the above line of code would echo out:

[email protected]

Of course, this looks bad. I can’t tell where the user_login ends and the email address begins. So I would need to either use regular text or html to add either a space between the words…

{code type=php}

echo $user_data[‘user_login’]  .  “ “ . $user_data[‘user_email’];

{/code}

… or use the html tag “<br/>” to create a full line break between the words:

{code type=php}

echo $user_data[‘user_login’] ;

echo “<br />”;

echo $user_data[‘user_email’];

{/code}

Now, applying everything we just covered in this tutorial and the previous tutorial in this series, you should now be able to decipher what this chunk of code is doing:

{code type=php}

//grab user post data//

$user_post_data = $wpdb->get_row(“SELECT * FROM $wpdb->posts WHERE post_author = ‘” . $user_data[‘ID’] . “‘”, ARRAY_A);

echo “<br />”;

echo $user_post_data[‘post_title’];

echo “<br />”;

{/code}

I’ve just stored some more database information into a new variable named “$user_post_data,” which will contain a SELECTION from the “posts” (aka, wp_posts) table “WHERE” the “post_author” column equals the “ID” column from my “$user_data” variable.

Remember that the “$user_data” variable contains the row from our “wp_users” table where the “ID” equals the number one. So if I use the “[]” characters just after the “$user_data” variable and place “ID” within the “[]” characters, I’m telling WordPress that the variable represents the “ID” from the user row which I yanked out of the “wp_users” table and stored in the “$user_data” variable.

So the above chunk of code is grabbing all the posts written by user number (ID) one and I’m echoing the post title from the “$user_post_data” row between two line breaks:

{code type=php}

echo “<br />”;

echo $user_post_data[‘post_title’];

echo “<br />”;

{/code}

Now, since we’ve simply added new code to our admin page by adding the new database code to our “YourBrandWPAdminPage” method, our admin area should now contain the echoed information about user #1, including their username, email and the title of their WordPress post:

 

Woohoo! Super Cool Stuff Coming Up Now

Congratulations, you now know enough about WordPress to do quite a bit with your basic plugin. Of course, we’ve yet to cover Javascript (very cool stuff coming) and we haven’t talked about a lot of the WordPress filters, actions or database functions. But if you keep up with the tutorials which will be coming next in this series, I think you’ll see that we’ve opened up a LOT of cool options already with what we’ve learned.

Just go back to the first post of this tutorial series and try out some of what you now know if you want to get an idea of what I mean. In the next tutorial, we’ll start modifying your plugin so that you and your users can actually “talk” to it, and preparing you for including Javascript.

More to come!

-Best,

Seth C

Other Posts in This WordPress Plugin Construction Series

How to Create Your Very First WordPress Plugin
WordPress Plugin Construction for the Non-Programmer: Part II
WordPress Plugin Construction for the Non-Programmer: Part III
WordPress Plugin Construction for the Non-Programmer: Part IV
WordPress Plugin Construction for the Non-Programmer: Part V