{"id":94529,"date":"2012-08-21T15:00:08","date_gmt":"2012-08-21T19:00:08","guid":{"rendered":"http:\/\/wpmu.org\/?p=94529"},"modified":"2013-04-17T14:47:13","modified_gmt":"2013-04-17T18:47:13","slug":"wordpress-plugin-construction-for-the-non-programmer-part-v","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/wordpress-plugin-construction-for-the-non-programmer-part-v\/","title":{"rendered":"WordPress Plugin Construction for the Non-Programmer: Part V"},"content":{"rendered":"<p>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\u2019re a total newbie to WordPress development.<\/p>\n<p>Just a couple things to keep in mind about this series:<\/p>\n<h2>Five Official \u201cCover my Butt\u201d Statements<\/h2>\n<ul>\n<li>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.<\/li>\n<li>I\u2019m assuming you already know how to install a basic WordPress site, if you\u2019re unsure please see their <a href=\"http:\/\/www.wordpress.org\/\" target=\"_blank\">www.wordpress.org<\/a>.<\/li>\n<li>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.<\/li>\n<li>If you\u2019re confused by any of the code or terminology above, please go back and read the first two posts in this series.<\/li>\n<li>No instant gratification in this lesson! We\u2019ll be talking about how to access the WordPress database so we can create even cooler plugins in future tutorials.<\/li>\n<\/ul>\n<h2>There\u2019s Nothing Worse Than a Plugin with Short Term Memory Loss!<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94547\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/08\/memory-loss.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"440\" height=\"300\" \/><\/p>\n<p>Okay, we\u2019ve talked about <a href=\"https:\/\/wpmudev.com\/blog\/how-to-create-your-very-first-wordpress-plugin\/\" target=\"_blank\">creating a very basic WordPress plugin<\/a> in the past four posts. Now, the time has come to talk about storing data so that we can give your WordPress plugin a \u201cmemory.\u201d By \u201cmemory,\u201d I mean that your plugin will be able to recall the data and the settings which you and your users communicate to it.<\/p>\n<p>Every WordPress site has a memory, it\u2019s called a \u201cMySQL database.\u201d<\/p>\n<p>This MY SQL database is like the WordPress site\u2019s \u201chard drive.\u201d It\u2019s 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.<\/p>\n<p>So unless you want to build a WordPress plugin that has Alzheimer\u2019s disease, you\u2019ll need to learn how to store and retrieve data from the WordPress database. Don\u2019t get scared by this. Database theory isn\u2019t nearly as complicated as some make it out to be. You probably already know more about it than you realize.<\/p>\n<p>Ever used Microsoft Excel?<\/p>\n<p>The basic structure of a database isn\u2019t much different. Just imagine each spreadsheet as a \u201ctable\u201d of data and the entire excel file as the database itself. For example, let\u2019s assume you\u2019ve got a very simple data base with this very simple table in it:<\/p>\n<p><strong>Table name: users<\/strong><\/p>\n<p><a rel=\"lightbox[94529]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wordpress-plugin-construction-for-the-non-programmer-part-v\/user-table\/\" rel=\"attachment wp-att-94575\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94575\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/08\/user-table.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"441\" height=\"215\" \/><\/a><\/p>\n<p><strong>Table name: post_content<\/strong>Now, let\u2019s assume you\u2019ve got another table within your database which contains your post content:<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94578\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/08\/content-table.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"438\" height=\"245\" \/>\u00a0<\/strong><\/p>\n<p><strong><\/strong>Can you figure out what\u2019s going on with the \u201c1\u201d in the \u201cauthor\u201d column of the \u201cpost_content\u201d table?<\/p>\n<p>It\u2019s referencing user #1 in the \u201cusers\u201d table as the author or blog post #1 in the \u201cpost_content\u201d table. This cross referencing is how a database \u201cremembers\u201d that Jack Leak wrote this post. This way, I don\u2019t have to keep storing Jack\u2019s name every time he posts something and I don\u2019t have to update BOTH tables every time Jack changes his name.<\/p>\n<p>Notice again the \u201c2\u201d in the \u201ccategory\u201d column of the \u201cpost_content\u201d table. Where did that come from? If you guessed that there\u2019s <em>another <\/em>table in our database named \u201ccategories\u201d and that the second \u201cid\u201d row contains the category which Jack put his \u201cMy Awesome Blog Post\u201d into, you\u2019re right.<\/p>\n<p>That\u2019s 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:<\/p>\n<ul>\n<li><strong>Access data <span style=\"text-decoration: underline\">already stored in the existing tables<\/span> of the WordPress database<\/strong><\/li>\n<li><strong><span style=\"text-decoration: underline\">Add more columns to an existing table<\/span><\/strong><strong> in the WordPress MY SQL database<\/strong><\/li>\n<li><strong><span style=\"text-decoration: underline\">Create a new table<\/span><\/strong><strong> in the WordPress database just for storing the data from your WordPress plugin<\/strong><\/li>\n<\/ul>\n<p>In this tutorial, we\u2019re doing to start with accessing information from the existing tables using WordPress commands. Once you get this concept down, you\u2019ll be ready to start thinking about applying the second two by creating your own columns and tables within the WordPress database.<\/p>\n<p><strong>How WordPress Plugins Access the WordPress Database<\/strong><\/p>\n<p>Remember that your first job in creating your WordPress plugin was to create your plugin class, which in this series we\u2019ve 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.<\/p>\n<p>It\u2019s 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 \u201c$wpdb\u201d variable as a global.<\/p>\n<p>*<em>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.<\/em><\/p>\n<p>I\u2019ll explain this in a bit more detail in a second. But first, let\u2019s go ahead and stick this new $wpdb tool into our class.\u00a0 Assuming that we\u2019re working with the two tables I showed you above and see if you can figure out whose data I\u2019m accessing with this method:<\/p>\n<p>{code type=php}<\/p>\n<p>function BestAuthor() {<\/p>\n<p>global $wpdb;<\/p>\n<p>$user_data = $wpdb-&gt;get_row(\u201cSELECT first_name, last_name FROM $wpbd-&gt;users WHERE id = \u20181\u2019);<\/p>\n<p>echo $user_data[\u2018first_name\u2019];<\/p>\n<p>}<\/p>\n<p><\/code><\/p>\n<p>If you call this function later within your class handler, it will echo out the name \u201cJack.\u201d Of course, this example won\u2019t work if you try it on your WordPress test site because the WordPress database tables are different than the ones we\u2019re using in this post. But this example is just for the sake of making the concept simple to grasp.<\/p>\n<p>Let\u2019s break down each line of this method in plain English so you can see what we\u2019ve actually done. First, we created a new method and named it \u201cBestAuthor.\u201d<\/p>\n<p>{code type=php}<\/p>\n<p>function BestAuthor() {<\/p>\n<p><\/code><\/p>\n<p>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 \u201cglobal,\u201d letting WordPress know that we were going to work with the \u201c$wpdb\u201d\u2019 class:<\/p>\n<p>{code type=php}<\/p>\n<p>function BestAuthor() {<\/p>\n<p>global $wpdb;<\/p>\n<p><\/code><\/p>\n<p>BTW, \u201cGlobal\u201d 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.<\/p>\n<p>Once we\u2019ve closed that line with the \u201c;\u201d character, we create a variable called \u201c$user_data\u201d 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\u2019s data from within the \u201cuser\u201d table within our database.<\/p>\n<p>Let\u2019s look at those commands again and see if you can spot how we asked for Jack\u2019s data:<\/p>\n<p>{code type=php}<\/p>\n<p>$wpdb-&gt;get_row(\u201cSELECT first_name, last_name FROM $wpbd-&gt;users WHERE id = \u20181\u2019);<\/p>\n<p>echo $user_data[\u2018first_name\u2019];<\/p>\n<p><\/code><\/p>\n<p>The \u201c$wpdb\u201d shows that we\u2019re using the WordPress database toolbox to select a row (get_row) within our database. We then \u201cSELECT\u201d the \u201cfirst_name\u201d and the \u201clast_name \u201c records \u201cFROM\u201d the table named \u201cusers\u201d (once again, using the \u201c$wpdb\u201d tool to access the database \u201cWHERE\u201d the \u201cid\u201d is equivalent to \u201c1.\u201d<\/p>\n<p>If you look at our users table again, you\u2019ll notice that Jack\u2019s id is equivalent to one, so we just grabbed Jack\u2019s first and last name out of that table. Then, the last line simply echoes out the \u201c[\u2018first_name\u2019]\u201d segment of our \u201c$user_data\u201d variable. If I\u2019d wanted to echo the last name as well, I would have simply added another line which reads:<\/p>\n<p><strong>echo $user_data[\u2018last_name\u2019];<\/strong><\/p>\n<p>What if I\u2019d wanted to display Jack\u2019s email as well?<\/p>\n<p>No problem, just add another line right?<\/p>\n<p><strong>echo $user_data[\u2018email\u2019];<\/strong><\/p>\n<p>Ah, but not quite.<\/p>\n<p>Check out our \u201c$user_data\u201d array (variables which contain strings of information are called \u201carrays\u201d) and you\u2019ll notice that we didn\u2019t ask the WordPress database for the user\u2019s email. So that echo command wouldn\u2019t give us any information. To get the result we\u2019re looking for, we\u2019d need to update our \u201cget_row\u201d function to request that data as well:<\/p>\n<p>{code type=php}<\/p>\n<p>$wpdb-&gt;get_row(\u201cSELECT * FROM $wpbd-&gt;users WHERE id = \u20181\u2019);<\/p>\n<p>echo $user_data[\u2018first_name\u2019];<\/p>\n<p><\/code><\/p>\n<p>That \u201c*\u201d character tells the WordPress database that I want to select everything from the users database where the column \u201cid\u201d is equal to the integer \u20181\u2019. If I\u2019d wanted to select the data based on the user\u2019s email instead of based on their id, I\u2019d rewrite my code to read like this:<\/p>\n<p>{code type=php}<\/p>\n<p>$wpdb-&gt;get_row(\u201cSELECT * FROM $wpbd-&gt;users WHERE email = \u2018jacksemail@url.com\u2019);<\/p>\n<p>echo $user_data[\u2018first_name\u2019];<\/p>\n<p><\/code><\/p>\n<p>This code ^ could be used to discover a user\u2019s name if you only have their email to work with. With this information, I could then search out <em>other <\/em>data within my database base which was either created by the user or which related to their user account.<\/p>\n<p>Now, we won\u2019t actually be making your WordPress Basic plugin do anything yet because it\u2019s important to understand this concept first. So let\u2019s take a few code examples and see if you can figure out what we\u2019re doing with these\u2026<\/p>\n<h2>Test Your New Knowledge of WordPress Database Theory<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-94548\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2012\/08\/test-your-knowledge.jpg\" alt=\"Post image\" aria-hidden=\"true\" width=\"440\" height=\"300\" \/><\/p>\n<p>Rather than just giving you the answer to these methods, I\u2019m going to ask you to leave your guesses in the comments below this blog post. Keep in mind \u00a0that we\u2019re grabbing information from the fictional database tables from earlier in this post:<\/p>\n<p><strong>Method #1:<\/strong><\/p>\n<p>{code type=php}<\/p>\n<p>function BlogTitle() {<\/p>\n<p>global $wpdb;<\/p>\n<p>\/\/grab post data\/\/<\/p>\n<p>$post_data = $wpdb-&gt;get_row(\u201cSELECT * FROM $wpbd-&gt;post_content WHERE id = \u20181\u2019);<\/p>\n<p>$post_title = $post_data[\u2018title\u2019];<\/p>\n<p>echo $post_title;<\/p>\n<p>}<\/p>\n<p><\/code><\/p>\n<p><strong>Method #2:<\/strong><\/p>\n<p>{code type=php}<\/p>\n<p>function BlogAuthor() {<\/p>\n<p>global $wpdb;<\/p>\n<p>\/\/grab post data\/\/<\/p>\n<p>$post_data = $wpdb-&gt;get_row(\u201cSELECT * FROM $wpbd-&gt;post_content WHERE id = \u20181\u2019);<\/p>\n<p>$author_id = $post_data[\u2018author_id\u2019];<\/p>\n<p>\/\/grab and display author name\/\/<\/p>\n<p>$author_name = $wpdb-&gt;get_row(\u201cSELECT first_name FROM $wpbd-&gt;users WHERE id = \u2018$author_id\u2019);<\/p>\n<p>echo $author_name[\u2018first_name\u2019];<\/p>\n<p>}<\/p>\n<p><\/code><\/p>\n<p><strong>Method #2:<\/strong><\/p>\n<p>{code type=php}<\/p>\n<p>function AuthorEmail () {<\/p>\n<p>global $wpdb;<\/p>\n<p>\/\/grab post data\/\/<\/p>\n<p>$post_data = $wpdb-&gt;get_row(\u201cSELECT * FROM $wpbd-&gt;post_content WHERE id = \u20181\u2019);<\/p>\n<p>$author_id = $post_data[\u2018author_id\u2019];<\/p>\n<p>\/\/grab and display author name and email\/\/<\/p>\n<p>$author_data = $wpdb-&gt;get_row(\u201cSELECT first_name, email FROM $wpbd-&gt;users WHERE id = \u2018$author_id\u2019);<\/p>\n<p>echo $author_data[\u2018first_name\u2019];<\/p>\n<p>echo \u201c \u201c . $author_data[\u2018last_name\u2019];<\/p>\n<p>echo \u201c \u201c . $author_data[\u2018email\u2019];<\/p>\n<p>}<\/p>\n<p><\/code><\/p>\n<p>If you can guess what each of these methods will echo out, you\u2019re ready to start giving your WordPress plugin a memory using the $wpdb class. Let\u2019s see how you do, post your responses below!<\/p>\n<p>-Till next time,<\/p>\n<p>Seth C<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial series requires no programming experience, you&#8217;ll simply learn how to build a WordPress plugin from scratch&#8230;<\/p>\n","protected":false},"author":132058,"featured_media":94533,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[131],"tutorials_categories":[],"class_list":["post-94529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-developers"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/94529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/132058"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=94529"}],"version-history":[{"count":0,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/94529\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/94533"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=94529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=94529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=94529"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=94529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}