an into to using $wpdb with your own database tables

26
Saturday, March 16, 13

Upload: vegasgeek

Post on 17-May-2015

894 views

Category:

Education


0 download

DESCRIPTION

Presented at WordUp AZ, this was a quick intro to using $wpdb to work with your own database tables from inside WordPress

TRANSCRIPT

Page 1: An into to using $wpdb with your own database tables

Saturday, March 16, 13

Page 2: An into to using $wpdb with your own database tables

HI, I’M JOHN

• Founder @ 9seeds.com

• WordPress Developer

• Viva, Las Vegas

Saturday, March 16, 13

Page 3: An into to using $wpdb with your own database tables

The ProblemYou’ve got database tables you

need to access from inside wordpress

Saturday, March 16, 13

Page 4: An into to using $wpdb with your own database tables

What are my options?

• Convert all the data to custom post types

• Write some (potentially) janky direct database access queries in to your theme or plugin

• Or, you can check out $wpdb

Saturday, March 16, 13

Page 5: An into to using $wpdb with your own database tables

What is $wpdb?

Saturday, March 16, 13

Page 6: An into to using $wpdb with your own database tables

$wpdb

“WordPress provides a global variable, $wpdb, which is an instantiation of the

class already set up to talk to the WordPress database.”

Saturday, March 16, 13

Page 7: An into to using $wpdb with your own database tables

Let’s dig in!

Saturday, March 16, 13

Page 8: An into to using $wpdb with your own database tables

My Contacts

Saturday, March 16, 13

Page 9: An into to using $wpdb with your own database tables

Some Data

Saturday, March 16, 13

Page 10: An into to using $wpdb with your own database tables

First upWe need to tell WordPress

about our new table

Saturday, March 16, 13

Page 11: An into to using $wpdb with your own database tables

Adding our table

Saturday, March 16, 13

Page 12: An into to using $wpdb with your own database tables

Accessing our table data

Saturday, March 16, 13

Page 13: An into to using $wpdb with your own database tables

get_results()

Saturday, March 16, 13

Page 14: An into to using $wpdb with your own database tables

Saturday, March 16, 13

Page 15: An into to using $wpdb with your own database tables

Which turns...

Saturday, March 16, 13

Page 16: An into to using $wpdb with your own database tables

Into...

Saturday, March 16, 13

Page 17: An into to using $wpdb with your own database tables

Adding data to our custom

Saturday, March 16, 13

Page 18: An into to using $wpdb with your own database tables

Start with a form

Saturday, March 16, 13

Page 19: An into to using $wpdb with your own database tables

Start with a form

Saturday, March 16, 13

Page 20: An into to using $wpdb with your own database tables

Check your nonce!

Saturday, March 16, 13

Page 21: An into to using $wpdb with your own database tables

... from the codex

All data in SQL queries must be SQL-escaped before the SQL query is executed to prevent against SQL injection attacks. This can be

conveniently done with the prepare method.

Saturday, March 16, 13

Page 22: An into to using $wpdb with your own database tables

query() and prepare()

Saturday, March 16, 13

Page 23: An into to using $wpdb with your own database tables

Our data added

Saturday, March 16, 13

Page 24: An into to using $wpdb with your own database tables

This is just the beginning!

Saturday, March 16, 13

Page 25: An into to using $wpdb with your own database tables

Resources

• http://codex.wordpress.org/Class_Reference/wpdb

• http://www.slideshare.net/vegasgeek

Saturday, March 16, 13

Page 26: An into to using $wpdb with your own database tables

Thank [email protected] - @vegasgeek

Saturday, March 16, 13