Author Archives: admin

WordPress | Add a style sheet to your theme quick and dirty

So you might need to add an additional css style sheet to your wordpress theme. If you are not developing a theme for the public and it is just you that is going to be working on the site and you just want to get it done quick and dirty then you can just navigate to your theme folder and open up your header.php file in your code editor and past in the link element that references your new css file.

Your final code will look something like this: –


<!-- all your header code -->

<link href="" media="all" type="text/css" />

Just add the link element just above the closing head tag and put your new css file in your theme directory. That would work but like I said it’s quick it’s dirty and I wouldn’t recommend it for anything other then a personal site.

I will be adding better and more complex ways to get this task done in future posts but if you really need to know now you can check out the enqueue style function in the wordpress codex


WordPress $wpdb basics

Access WordPress Database

Starship $wpdb

Recently I’ve been adapting a plug-in to suit my needs and for the first time, trying to write something that interacts with the wordpress database, step up wpdb class!

The WPDB Class – Make it Global

To use the wpdb class we need to access a global variable $wpdb which is simple enough, but what you’ve got to remember is that it needs to be globalized before you use it in your custom function: –

global $wpdb;

Once that’s in place you are free to use it to perform SQL queries and function on your database.

The first thing I needed to do was to query the “commentmeta” table in the database, and save the results for later use: –

$comment_meta = $wpdb->get_results( "SELECT mydata FROM mytable WHERE mycondition = 'myvalue' ");

The above code uses ‘get_results’ to query the database and loads in to the the variable $comment_meta so it can be used in a ‘foreach’ loop later. Now I wanted to grab the meta_key, meta_value from table ‘commentmeta’ that was named ‘wp_1_commentmeta’. I could have put that value into the query but if I want to use the function on a different site on a wordpress network I’m not going to know the database prefix so I can use $wpdb to replace it: –

$comment_meta = $wpdb->get_results( "SELECT meta_key, meta_value FROM wpdb->commentmeta ");

That way I can use the query in a function on any wordpress blog and it will always knows what table to look at.

It’s an Object by default

One last thing to remember is that the results are an object so you can access the like this: –

foreach ( $comment_meta as $comment_meta_data )
echo $comment_meta_data->meta_key
echo '< br />'
echo $comment_meta_data->meta_value

And that’s what I wanted but if you want something different like an array then you can use OBJECT_K for an associative array of row objects, using first column’s values as keys, ARRAY_A for a numerically indexed array of associative arrays, using column names as keys or ARRAY_N for a numerically indexed array of numerically indexed arrays like this: –

$comment_meta = $wpdb->get_results( "SELECT meta_key, meta_value FROM wpdb->commentmeta", ARRAY_N );

for more information on the wpdb class check out the wordpress codex