Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Creating new tables

Creating new tables provides you with in-depth training on Developer. Taught by Drew Falkman as part… Show More

WordPress: Creating Custom Widgets and Plugins with PHP

with Drew Falkman

Video: Creating new tables

Creating new tables provides you with in-depth training on Developer. Taught by Drew Falkman as part of the WordPress: Creating Custom Widgets and Plugins with PHP
Expand all | Collapse all
  1. 1m 49s
    1. Welcome
      1m 16s
    2. Using the exercise files
  2. 23m 29s
    1. WordPress overview
      2m 32s
    2. Installing WPI for Windows
      3m 42s
    3. Installing MAMP for the Mac
      3m 25s
    4. Installing and configuring WordPress
      5m 51s
    5. Comparing WordPress 3.0 with previous versions
      2m 57s
    6. Setting up a PHP/WordPress development environment
      5m 2s
  3. 14m 47s
    1. Exploring WordPress plugins
      3m 42s
    2. Administering plugins from the WordPress admin
      5m 23s
    3. Exploring where plugins reside
      2m 51s
    4. Introduction to hooks
      2m 51s
  4. 39m 28s
    1. Creating the plugin PHP file(s)
      3m 12s
    2. More on hooks: Actions and filters
      3m 15s
    3. Installation and activation
      4m 6s
    4. Writing activation code
      3m 45s
    5. Writing an action
      5m 12s
    6. Writing a filter
      4m 15s
    7. About pluggable functions
      2m 1s
    8. Writing a pluggable function
      5m 30s
    9. Using template tags
      2m 46s
    10. Introducing shortcode
      5m 26s
  5. 26m 2s
    1. Widgets and the WordPress Widgets SubPanel
      2m 54s
    2. Comparing widgets and plugins
      1m 8s
    3. Using and customizing built-in widgets
      3m 18s
    4. Creating a new widget
      7m 21s
    5. Writing the constructor and registering widgets
      5m 20s
    6. Enabling configuration of widgets
      6m 1s
  6. 44m 59s
    1. Creating an admin interface
      5m 25s
    2. Saving data to the database
      5m 39s
    3. Securing form submission with nonces
      2m 25s
    4. Options editing post-WordPress 2.7
      4m 8s
    5. Integrating with the WordPress admin menus
      3m 34s
    6. WordPress admin dashboard API
      4m 5s
    7. Using existing options and option editing pages in WordPress
      5m 19s
    8. Using jQuery and AJAX for administration
      14m 24s
  7. 27m 13s
    1. Accessing the WordPress database
      5m 45s
    2. Using the built-in schema
      2m 21s
    3. Accessing data using $wpdb
      5m 15s
    4. Creating new tables
      7m 18s
    5. Inserting data
      6m 34s
  8. 26m 27s
    1. Introducing the Loop
      6m 22s
    2. Using WP_Query()
      3m 11s
    3. Custom filtering and sticky posts
      4m 58s
    4. Using jQuery and AJAX for posts and pages
      11m 56s
  9. 12m 9s
    1. Registering and promoting plugins
      2m 28s
    2. Creating an uninstall function
      5m 53s
    3. Backward compatibility issues
      3m 48s
  10. 15m 3s
    1. Understanding security issues
      11m 20s
    2. Internationalizing your plugin
      3m 43s
  11. 18s
    1. Goodbye

please wait ...
Creating new tables
Video Duration: 7m 18s 3h 51m Intermediate


Creating new tables provides you with in-depth training on Developer. Taught by Drew Falkman as part of the WordPress: Creating Custom Widgets and Plugins with PHP

View Course Description

In WordPress: Creating Custom Widgets and Plugins with PHP, Drew Falkman teaches PHP developers how to create custom functionality for WordPress 2.0 through 3.0 using widgets and plugins. This course starts by installing and setting up WordPress 3.0 on both Mac and Windows, then provides an in-depth look at tasks related to these WordPress add-ons: installing and administering, building and customizing, creating editable options and database tables, working with posts and pages, and utilizing jQuery and AJAX. There are also tutorials dedicated to promoting a widget or plugin, adding security, and localizing the interface. Exercise files are included with the course.

Topics include:
  • Installing WPI and MAMP server solutions
  • Administering WordPress plugins
  • Introducing hooks
  • Writing the PHP for a plugin
  • Using template tags and shortcode
  • Building a new widget
  • Creating an admin interface
  • Accessing the WordPress database
  • Using jQuery and AJAX for posts and pages
  • Registering and promoting plugins
Developer Web

Creating new tables

The reason we've been looking at the WordPress database and why we might want to use it is because oftentimes our plugins might actually need their own data structures. Think about a program that collects statistics, or maybe you want to build a whole product database to display and integrate for a client that you have. Whatever the reason, WordPress gives us a bunch of tools to create our own tables in the WordPress database. To create our data structure, we're going to need to write a function that creates it in SQL. I've created here a plugin, and this will basically save the user agent from the browsers when they come to the site.

We can then parse it out later and show whatever statistics we want about what types of browsers are visiting our site, whether people are using iPhone, Android, or a regular computer. So, in order to access the database, I need to globalize the wpdb variable, so that I can access it in my function. Then I'm going to create a name for my table. So I have a variable that I'm creating called table_name, and I'm going to use this special property called prefix, to get the prefix of the database. This is a best practice, because oftentimes people will have a special prefix they store in their WordPress installation.

So that way every table inside of the WordPress database will have a prefix. It's usually WP by default, but this allows them to have databases that share WordPress but also other functionality as well. And we're going to create our own table name and call it "bdetector" to store our information. So now what we're going to do is we're going to go in and determine if the table already exists. If it exists, then we won't create it. However, if it doesn't exist, we're going to want to create it. So, I'm going to create an if statement, and the way I determine if the table exists is I'm going to use the wpdb class, and I'm going to call the get_var.

If you remember, this is what I used to get a single field from the database. Now I'm going to run some SQL, and particularly I'm going to call 'SHOW TABLES LIKE ', and then I'm going to append on my table_name. So, if this is not equal to my table_name, then that means it didn't find it in the database. So it's either going to be the table_ name if it exists, or it's going to be empty if it doesn't exist. If it doesn't exist, I'm then going to run my query. So first I'm going to set up my query.

I'm just going to create a variable called sql, and I'm going to write it out. So we're going to say 'CREATE TABLE ', and remember you always want to have a space in here. Otherwise, when you append variables, the text is going to run up against one another and cause database errors. So, I'm going to put my table_name in here, and now I'm going to write out what the table looks like. There's going to be an id, which is going to be an INTEGER of size 10. It's UNSIGNED; in other word, it is not going to be negative. And we're going to set it to be AUTO_INCREMENT.

This is our key essentially. I'm going to have a hit_date. So I'm going to store when the user came, that's going to be a TIMESTAMP, and we're going to default it to CURRENT_TIMESTAMP. What that means is the database is automatically going to create this value if one doesn't exist for the current time. So, all I need to do is insert a record in here, and it will timestamp it for me. The user_agent is what I'm going to store, and that I'm going to get from the browser. And that's going to be a string, and I'm just going to give it 255.

Most of them aren't going to be that long, but it gives me some flexibility. And then lastly, I'm going to declare my PRIMARY KEY to be the id. You may need to make sure that there is a couple of spaces in there. Okay, so there is my query. Now, in order to actually do this, I'm going to use this special function called dbDelta. This offers some assistance. Essentially what it will do is it'll take this pattern here--and you want to write it just like this with a new line for each of the columns you're going to create in the database-- dbDelta will essentially, if there is any changes, it will update the table.

If not, it will just create it as is. If it's the same, it won't do anything. It's a very helpful function. In order to use it, I'm going to have to include it, and the way we include things is to use this require_once function. I'll specify the absolute path, which is the absolute path of my WordPress installation. Then I'm going to say in the WordPress admin directory, in the includes directory, and it's called upgrade.php. Incidentally, when you create your own plugins, you're going to want to use this same methodology to include additional files if you break it up into multiple files.

So now that we've included this, we can call dbDelta, and we'll pass to it our query. So, this will then create our database. Another good idea to do, and this is optional, is we're going to add an additional option. So we're going to use the option database to set up an option so that we can have a version of this database, so that if we do want to do updates to this particular plugin in the future, we can simply look at our option and see what version the database is and update if necessary.

So, I'm going to call the add_ option function, and I'm going to call it bdetector, so it's unique and then database_version, and we'll just set it to 1.0. So, now that I have all this set up, I'm going to make sure that this runs for the activation action. So, if you recall, the way we do that is we call register_activation_hook. We pass the file, and we can use file, which represents the current file, and then the function that we want to execute when we activate this plugin.

So, bdetector_activate. So that's all we need. This is our plugin, and this will essentially create the database. Let's go ahead and go into our WordPress admin, go to the Plugins page, find our Browser Detector Plugin, which is right here, and go ahead and activate it. When it was activated, it should have created this table in the database. So let's go ahead and look at our MAMP installation. Let's go into phpMyAdmin, look at wp _test, and you can see we have this table wp_bdetector.

When I click on it, you can see it created an id, a hit_date, and a user_agent. So, to create your own database in WordPress, it's a good idea to use that dbDelta class in upgrade.php. This is going to upgrade the table using SQL query, and typically we're going to add this as an activation hook. And now you've got your own database table, so you can start storing things with your plugins. Creating your own database table in WordPress is a fairly straightforward process. You need to create your SQL that's going to create the table using the CREATE TABLE statement.

Make sure in your table_name, you use the wpdb prefix property to get the prefix that the user is using for their WordPress installation. When your statement is ready, simply call the dbDelta function that you can find in the upgrade.php class. Now let's go on and look how we can insert and update data into our database table.

Find answers to the most frequently asked questions about WordPress: Creating Custom Widgets and Plugins with PHP .

Expand all | Collapse all
please wait ...
Q: Do I need a web hosting service for this course?
A: You don't need a hosting site to do any testing or development work that’s covered in this course. However, if you want to have your WordPress site available to the public, you will most definitely need a WordPress site. If you are hosting with an independent company, they will need to have PHP and MySQL installed, and there will be some configuration differences, but basically, you can upload anything on your local version to the web site. If you are hosting with, you will need to add your plugins by uploading them manually through the WP Admin Plugin screen.





Don't show this message again
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed WordPress: Creating Custom Widgets and Plugins with PHP.

Return to your organization's learning portal to continue training, or close this page.


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

Join today and get unlimited access to the entire library of online learning video courses—and create as many playlists as you like.

Get started

Already a member?

Log in

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.