Quantcast
Channel: wp-coder.net » admin
Viewing all articles
Browse latest Browse all 10

WordPress Plugin Construction for the Non-Programmer: Part V

$
0
0

Welcome to the fifth 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:

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 two posts in this series.
  • No instant gratification in this lesson! We’ll be talking about how to access the WordPress database so we can create even cooler plugins in future tutorials.

There’s Nothing Worse Than a Plugin with Short Term Memory Loss!

Okay, we’ve talked about creating a very basic WordPress plugin in the past four posts. Now, the time has come to talk about storing data so that we can give your WordPress plugin a “memory.” By “memory,” I mean that your plugin will be able to recall the data and the settings which you and your users communicate to it.

Every WordPress site has a memory, it’s called a “MySQL database.”

This MY SQL database is like the WordPress site’s “hard drive.” It’s how WordPress recalls post content, page content, comments and other data which you and your users input. Without its memory, WordPress would never be able to remember you or your users or the post content you worked your fingers to the bone to publish.

So unless you want to build a WordPress plugin that has Alzheimer’s disease, you’ll need to learn how to store and retrieve data from the WordPress database. Don’t get scared by this. Database theory isn’t nearly as complicated as some make it out to be. You probably already know more about it than you realize.

Ever used Microsoft Excel?

The basic structure of a database isn’t much different. Just imagine each spreadsheet as a “table” of data and the entire excel file as the database itself. For example, let’s assume you’ve got a very simple data base with this very simple table in it:

Table name: users

Table name: post_contentNow, let’s assume you’ve got another table within your database which contains your post content:

 

Can you figure out what’s going on with the “1” in the “author” column of the “post_content” table?

It’s referencing user #1 in the “users” table as the author or blog post #1 in the “post_content” table. This cross referencing is how a database “remembers” that Jack Leak wrote this post. This way, I don’t have to keep storing Jack’s name every time he posts something and I don’t have to update BOTH tables every time Jack changes his name.

Notice again the “2” in the “category” column of the “post_content” table. Where did that come from? If you guessed that there’s another table in our database named “categories” and that the second “id” row contains the category which Jack put his “My Awesome Blog Post” into, you’re right.

That’s a very basic explanation of how databases are structured and how the tables within a data base might communicate with one another. The easy part is that WordPress has already created a series of commands which your plugin can use to either:

  • Access data already stored in the existing tables of the WordPress database
  • Add more columns to an existing table in the WordPress MY SQL database
  • Create a new table in the WordPress database just for storing the data from your WordPress plugin

In this tutorial, we’re doing to start with accessing information from the existing tables using WordPress commands. Once you get this concept down, you’ll be ready to start thinking about applying the second two by creating your own columns and tables within the WordPress database.

How WordPress Plugins Access the WordPress Database

Remember that your first job in creating your WordPress plugin was to create your plugin class, which in this series we’ve been compared to building a toolbox. Well, as it happens, WordPress already has a few toolboxes (classes) of its own, one of which makes it easy for you to access the WordPress database.

It’s called the $wpdb class, and just as you assigned a handler to the toolbox which you created for YOUR plugin, you can now assign another handler to go to work using the wpdb toolbox. You do this by creating a new method* (tool) within your already existing class using the “$wpdb” variable as a global.

*Remember that methods are also called functions in PHP. The only difference is that a method is used within a class and a function is used by itself.

I’ll explain this in a bit more detail in a second. But first, let’s go ahead and stick this new $wpdb tool into our class.  Assuming that we’re working with the two tables I showed you above and see if you can figure out whose data I’m accessing with this method:

function BestAuthor() {

global $wpdb;
$user_data = $wpdb->get_row(“SELECT first_name, last_name FROM $wpbd->users WHERE id =1);
echo $user_data[‘first_name’];
}

If you call this function later within your class handler, it will echo out the name “Jack.” Of course, this example won’t work if you try it on your WordPress test site because the WordPress database tables are different than the ones we’re using in this post. But this example is just for the sake of making the concept simple to grasp.

Let’s break down each line of this method in plain English so you can see what we’ve actually done. First, we created a new method and named it “BestAuthor.”

function BestAuthor() {

Then, we defined exactly what this method would do by enclosing some code between the { and the } characters. First, we called the $wpdb variable as a “global,” letting WordPress know that we were going to work with the “$wpdb”’ class:

function BestAuthor() {


global $wpdb;

BTW, “Global” simply means that the variable can be used anywhere within the WordPress install, rather than being good only within a function or a class itself. Since $wpdb has been defined in WordPress as a global, we call it as a global.

Once we’ve closed that line with the “;” character, we create a variable called “$user_data” and store some information within that variable just as I explained in the first post of this series. However, this time we used a few commands to grab Jack’s data from within the “user” table within our database.

Let’s look at those commands again and see if you can spot how we asked for Jack’s data:

$wpdb->get_row(“SELECT first_name, last_name FROM $wpbd->users WHERE id =1);
echo $user_data[‘first_name’];

The “$wpdb” shows that we’re using the WordPress database toolbox to select a row (get_row) within our database. We then “SELECT” the “first_name” and the “last_name “ records “FROM” the table named “users” (once again, using the “$wpdb” tool to access the database “WHERE” the “id” is equivalent to “1.”

If you look at our users table again, you’ll notice that Jack’s id is equivalent to one, so we just grabbed Jack’s first and last name out of that table. Then, the last line simply echoes out the “[‘first_name’]” segment of our “$user_data” variable. If I’d wanted to echo the last name as well, I would have simply added another line which reads:

echo $user_data[‘last_name’];

What if I’d wanted to display Jack’s email as well?

No problem, just add another line right?

echo $user_data[‘email’];

Ah, but not quite.

Check out our “$user_data” array (variables which contain strings of information are called “arrays”) and you’ll notice that we didn’t ask the WordPress database for the user’s email. So that echo command wouldn’t give us any information. To get the result we’re looking for, we’d need to update our “get_row” function to request that data as well:

$wpdb->get_row(“SELECT * FROM $wpbd->users WHERE id =1);
echo $user_data[‘first_name’];

That “*” character tells the WordPress database that I want to select everything from the users database where the column “id” is equal to the integer ‘1’. If I’d wanted to select the data based on the user’s email instead of based on their id, I’d rewrite my code to read like this:

$wpdb->get_row(“SELECT * FROM $wpbd->users WHERE email = ‘jacksemail@url.com’);
echo $user_data[‘first_name’];

This code ^ could be used to discover a user’s name if you only have their email to work with. With this information, I could then search out other data within my database base which was either created by the user or which related to their user account.

Now, we won’t actually be making your WordPress Basic plugin do anything yet because it’s important to understand this concept first. So let’s take a few code examples and see if you can figure out what we’re doing with these…

Test Your New Knowledge of WordPress Database Theory

Rather than just giving you the answer to these methods, I’m going to ask you to leave your guesses in the comments below this blog post. Keep in mind  that we’re grabbing information from the fictional database tables from earlier in this post:

Method #1:

function BlogTitle() {

global $wpdb;
//grab post data//

$post_data = $wpdb->get_row(“SELECT * FROM $wpbd->post_content WHERE id =1);
$post_title = $post_data[‘title’];
echo $post_title;
}

Method #2:

function BlogAuthor() {

global $wpdb;
//grab post data//

$post_data = $wpdb->get_row(“SELECT * FROM $wpbd->post_content WHERE id =1);
$author_id = $post_data[‘author_id’];
//grab and display author name//

$author_name = $wpdb->get_row(“SELECT first_name FROM $wpbd->users WHERE id = ‘$author_id’);
echo $author_name[‘first_name’];
}

Method #2:

function AuthorEmail () {

global $wpdb;
//grab post data//

$post_data = $wpdb->get_row(“SELECT * FROM $wpbd->post_content WHERE id =1);
$author_id = $post_data[‘author_id’];
//grab and display author name and email//

$author_data = $wpdb->get_row(“SELECT first_name, email FROM $wpbd->users WHERE id = ‘$author_id’);
echo $author_data[‘first_name’];
echo “ “ . $author_data[‘last_name’];
echo “ “ . $author_data[‘email’];
}

If you can guess what each of these methods will echo out, you’re ready to start giving your WordPress plugin a memory using the $wpdb class. Let’s see how you do, post your responses below!

-Till next time,

Seth C

 

Related posts:

  1. WordPress Plugin Construction for the Non-Programmer: Part III This is the third post in my series on WordPress…
  2. WordPress Plugin Construction for the Non-Programmer: Part IV Check out this “in plain English” tutorial on how to…
  3. WordPress Plugin Construction for the Non-Programmer: Part II How to create your very first WordPress plugin! This tutorial…


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images