Category Archives: News

Things, Goings On, and Stuff I write

Writing a WordPress Plugin: Part II

In Part One we discussed adding actions and filters to WordPress as an initial step in writing a WordPress plugin. These hooks into the WordPress API allow your code to be called by WordPress as certain events and code are generated.
However, your plugin may need to access the WordPress database to use WordPress’ data for outputting various pieces of information. WordPress’ $wpdb object is perfect for the job and easy to use.

The WordPress database interaction object is created in the WordPress code for you, so all you’ll need to do is import it into your functions that are called by WordPress like so:

function suckMyDatabasesBrainsOut(){
global $wpdb, $tableposts;
// other code here to make the world a better place
}

You’ll also need to import the database name that you’ll need access to because WordPress 1.2 uses variables to hold the database names. The database names are dynamic because you can have multiple WordPress installations and each installation has a different prefix. The list of default tables are found in the wp-settings.php file:

  • $tableposts
  • $tableusers
  • $tablesettings
  • $tablecategories
  • $tablepost2cat
  • $tablecomments
  • $tablelinks
  • $tablelinkcategories
  • $tableoptions
  • $tableoptiontypes
  • $tableoptionvalues
  • $tableoptiongroups
  • $tableoptiongroup_options
  • $tablepostmeta

You’ll have to determine what table has the data you’re after. I recommend using PHPMyAdmin to look at the information in your database.

OK, so you’ve got access in your plugin, but now you need data, because access is [only] half the battle (to mutilate G.I. Joe). You can use the following functions to get data from WordPress and to control how the WordPress Database object controls output [found in the wp-includes/wp-db.php file]:

$wpdb->escape($string)
Returns a string escaped for safety in MySQL queries.
show_errors()
Turns on WordPress returning MySQL error reporting when errors are encountered.
$wpdb->hide_errors()
Turns off WordPress’ returning MySQL errors when they are encountered.
$wpdb->flush()
Sets cached query results to null
$wpdb->query($SQLQuery)
Used to execute DELETE or UPDATE queries
$wpdb->get_var($SQLQuery, $x, $y)
Use this to get one value from the database, where $x and $y are specific data values. You can call this function with just the query if one result is expected from the query. I have found no documentation regarding the second and third parameter and no where are they used in the WordPress code for version 1.2.
$wpdb->get_row($SQLQuery)
Returns one database result row from a query.
$wpdb->get_col($SQLQuery)
Returns the values from a table column for use as a PHP array.
$wpdb->get_results($SQLQuery, $objectForValueToBeReturnedIn)
Returns the SQL query results in an object that can be accessed by the result column names. If you don’t care what object is used for access you can skip the second parameter.
$wpdb->get_col_info($informationType, $columnOffset)
Returns column meta information for the last column requested. $informationType defaults to 'name' and $columnOffset defaults to -1.

Remember that the data returned by the $wpdb->get_results($SQLQuery)

Notes:It should be noted that WordPress 1.3 changes database access by only requiring you to import the $wpdb global. Currently no information has been published that explains how to add external databases (for example the StatTraq statistics database).

White Castle Update Update

OK, so after about 3 hours I’ve been assimilated and now I must eat White Castle all the time. I think I will buy one every day I love them they’re great. I think they’re better than Chipotle or Ruth’s Chris… NOT! I am chewing gum because the intense smell of my burps makes this really a bad plan for me to repeat it.

In the end I can now say that I have been there, done that and bought the Peptobismol.

White Castle Update

I ate two (2) White Castle (Motto: “It’s what you crave”) Burgers today (Reality: “It’s what passes for edible for this cheap”). It was everything I expected: yuck. I ate two because I wanted to give them a chance. Also, as Trint mentioned in my comments for the first post, there is very little substance to these ‘burgers’. They’re more like mini burgers that they serve at the Cheesecake Factory (CF) except you don’t mind eating the ones at the CF. The smell in the car (I got them ‘to go’) made all of my passengers complain.

My father-in-law, John, suggests that I will still be tasting these ‘burgers’ in 5 hours. I’ll let you know (I added too many parenthetical statements to this post).

Eels and Escalators

This morning Abby wanted an eel. Not just any eel, but the red one with spots on it. Grandpa looked blankly at the counter where Abby was pointing wondering what the eel was. It is my duty as a father to know what it is she wants: dice. There were two dice on the counter and Abby wanted them to play eels and Escalators. Eels and Escalators is a game on a Sponge Bob Squarepants episode (“Sailor Mouth” for those of you keeping track).

Who’d’a thunk it?

Completely Tired… Again

It is 12:45 and I just lost at Scrabble. John was 35 points ahead of Jessica and she was 5 points ahead of me. That is the first loss in Scrabble so far this week 🙂 I tried, but John kept laying down great words and scoring better and better. However, I did win at losing. I lost every game I played today 🙂 Today I lose games… tomorrow I lose the world!

White Castle

I have never eaten at a White Castle Hamburger Restaurant. Today, yet again, we passed one and I mentioned I have not eaten there. Every time I mention this (which is rarely) all those present say, “You’re lucky!” Or other phrases presenting to me the general idea that White Castle is not America’s favorite fast food chain. The other thing that happened after saying this was that Jessica said, “Oh, My gosh! He talks about that all the time.” This sort of inference is wrong and should not be made… I talk about it each time I pass one… mostly to see if Jessica will remind me that I’m going to be disappointed.

Right now my standards for White Castle are so low [given their reputation] that I think that as long as it’s not manure with spit between two pieces of moldy bread I’ll be impressed.

Oh, speaking of potty humor, this morning Abigail was waiting for her bath while Jessica was going to the bathroom and when Jessica had finished Abigail exclaimed, “Good job, you went to the potty, Mommy!” The potty training must be working 🙂

Zoo II

Today we went to the zoo with Abby and she had fun. What was really cool was the sea horse exibit. We found one non-exibit thing to be hugely amusing: We saw one gal in her early twenties wearing a bikini top – and about one corner away came a group of Mennonites (it apeared that there were 5-6 families… but that’s a guess). I’m sure this caused some eyes to be raised. If they took the commandment to gouge out your eye if it causes you to sin then I’m pretty sure the wives wanted to poke some eyes out and put a full length dress on the gal. The lions were roaring and the Abby got to ride the merry-g0-round – so all had a good time :).

Top Five Reasons Not to Go to Plainfield Indiana

  1. You are allergic to corn
  2. You’re trying to get to Arizona from Colorado
  3. You’re looking for Disney Land
  4. You couldn’t find a Starbuck’s anywhere else
  5. You want to spend more time with your sister-in-laws*

Actually, we’ve had a great time and are glad we’re here. However, my sister-in-laws are glad to be home and with their friends. Today we went for a long walk through part of Plainfield and really enjoyed it. Abby was pretty pooped by the time we were heading home, but she was really having a good time and enjoyed the neat park nearby. Plainfield is developing into a neat little town, if it keeps developing like this it will turn into a city, but right now it’s at a pretty neat place in its history. We’ll see what else we see and happens in the next 5 days. I’ll keep you posted.

* The sister-in-laws that live in Plainfield

The Trip

Well, you could define ‘trip’ two ways: a travelling experience and to be weirded out by something (as in a drug trip). This trip was really, really, really weird. Picking up the rental car the gal at (dis-)Advantage rent a car asked me where I was going.

“Indianapolis, Indiana,” I replied.

“Will you be going through Arizona on your way?”

“Um, no.”

Not the brightest bulb in the basket.

And then last night at 1:00 AM when I was completely exhausted we checked into a ‘Best Western.’ Best is an overstatement. Western was wrong, we’re in the mid-west. The pillow case was missing on one of the pillows, but I just put my shirt over it and went to sleep. Then, in the morning we discovered that the shower drain drained about 2 ounces per second. This means that you take a shower while the water collects and your feet mingle with the water that is mingling with the drain scum that is clogging it. GROSS!

However, conversation was fun, Abby was well behaved and we made it in good time.

All is well – we’re in Plainfield now.