Today we’re going to talk about manipulating data in WordPress without using the database. Hacking WordPress is great, because basically you learning to add and modify things in your theme with little code snippets. WordPress plugins are great, but so many WP newbies get caught up in “plugin overload”.

When you add (yet another) plugin to WordPress you are actually adding to it’s load. Meaning, every single time a page loads in WordPress it has to check every single plugin in /wp-content/plugins (EVEN the ones that are NOT activated!) to make sure it doesn’t have to do anything. If you have 5 plugins WP has to check 5 times, if you have 35 plugins it has to check 35 times! Also, nearly every plugin you activate adds data and/or tables to your database. More data, more tables, more queries = MORE LOAD. Is it any wonder why some WP powered blogs and web sites are soooooo slow?

Some of your activated plugins might not be coded optimally and might have excessive database queries that will slow you down. Some plugins conflict with other plugins as well. My general rule of thumb is, – if it’s something I can figure out how to add a little snippet of code for, I will! For instance, I know many people that use a google analytics plugin – and to that I say -WTF? The FIRST thing you should do is learn how to use your theme editor in the WP dashboard. If you can’t copy and paste some simple javascript into your footer through the dashboard – you’ve GOT to learn how to do it and become wordpress literate ASAP!

Now then – let’s get started!

How to ‘include’ a file Anywhere

One of the easiest things you can learn in PHP is how to ‘include’ the contents of a file. Why install a plugin for something as simple as this?

Open up a text editor (NOT word processor like Word, TEXT editor like Notepad, etc.) and put something in it. It doesn’t matter what it is, it can be words, HTML or PHP code, JavaScript, whatever. Save it on your desktop as “my-include.php”. Upload it to your theme directory.

Now, open your “index.php” and place the following code before or after the loop:





Guess what – you just learned your first data hack! Typically an “include” like this is used for an instance where you want to display the same thing on in many different areas or theme pages. Maybe you want the same google adsense block to display on all your tag and category pages? Just use a one line include before the loop to include one file with the JavaScript code for adsense! Maybe you want the same banner ad, or announcement on certain pages, an include will save you there too.

Conditional Includes

It’s VERY easy to create a much more streamlined include by using WordPress conditional includes.

Maybe you want to include an ad, an images, or some text in your sidbar, but ONLY only on category and tag pages.

This is the code:





With conditional tags you can include the file on only specific pages, you could have a message on just your home page in the header, footer, or sidebar – the possibilities are endless. I wrote an article over at wphacks.com about this: learn more about Conditional Tags.

Adding Data To ‘the loop’

I showed you how to add things before and after your content – now let’s try and add some random stuff right in the loop! My best example for this is ads, on my many different homepages on my blogs I tend to add adsense ads in between the posts. Problem is – adsense doesn’t like more than 2 of these per page! So you can’t have the “include” happen after every listed file – you have to do something like this:








All that code says basically is to include my file “adsense_homepage.php” after the second and sixth post in the loop!

How to Get data in a file

The last thing I have to show you is a little more complex example for getting the contents of a file line by line. Basically this code will grab exactly one (random) line from a file and include it. This is great if you want to create a file with say a dozen images, and include a different one in your header randomly each time the page loads. I use it rotate different text and banner ads all the time.

There’s only two things you need to know – first EVERYTHING you want to include has to be on exactly ONE line per item.

Here’s the code:



 0) {
$ad = ABSPATH . "wp-content/" . $ad . '.php';
if(file_exists($ad)) {
$ads = file($ad);
return $ads[rand(0, sizeof($ads)-1)];
}
}
}
?>

You don’t have to make any changes to the code “except” for the very first line. Create a file called “my-ads.php” and save it in your /wp-content directory. You only need to change getad(my-ads) to getad(my-filename) if yours isn’t called “my-ads.php”. Just make sure your filename has the .php extention or you’ll have to change that in the code above as well!

I’m sure I’ll get a comment saying you could just ad this to your WordPress functions file or make a plugin out of this – sure you could, I’m just trying to keep this example simple.

Conclusion

There you have it, everything you need to know to get you started hacking file includes in your WordPress theme! So go start making some customizations and stop downloading so many plugins!