Writing WordPress Plugins and Hooks

Original can be found here

Hooks: why?
————-

When you write a plugin you want to be able to switch it on and off whenever you want. This is not that trivial: say you write a function that does something useful, simply calling the function from your code gives an error when your plugin is deactivated. If you are lucky, it displays nothing, if you are less fortunate it displays all kinds of ugly debug information that people can abuse.

That is why ‘hooks’ were invented. Simply put, a hook is a point somewhere in the generation of your page where you can attach functions. When the hook is reached, all functions attached to it are executed in order of priority. When there are no functions attached to it, it does nothing.

So a plugin should do two tasks:

  • define a useful function
  • attach that function to the right ‘hook’

Many hooks exist already, and most people stick to those hooks. However, hooks are not black magic. Nothing stops you from defining your own hooks, and that is exactly what I’ll do in this page: I’ll create a trivial plugin, put it on my own hook at a place in the page where I like it.

Adding a hook
————–

The first thing I’ll do is the thing many people fear: I’ll create a hook. Using the default theme as an example, the page has a div called "content" which contains the posts in one page. I might want something to happen on the top of the "posts" section, maybe a status message, or a ‘recent comments’ section. That would be the right place to add my own hook.


Now before do that, there is some danger here: many hooks exist already, and before everyone starts defining their own hooks with the same names, let’s set a rule here: if you define your own hook, it should start with your own initials, to let the world know that it works for your plugins and to avoid name collisions. If it is a useful hook, it might become a standard hook for everyone one day.

Ok. Now let’s dive into the "default" theme directory (of course, we work on a backup) and start hacking on "index.php". The spot I had in mind was on line 4, right below the "content" div. I create a hook here called ‘lv_content_head’, because ‘lv’ are my initials, and it is the head of the content. I don’t use any parameters for now.

Below line 3 of "index.php", right below the "content" div, add the following line:

< ?php do_action("lv_content_head"); ?>

That’s all. If you load the page, you’ll see that nothing special happened. That is as it should, because there are no functions attached to it yet.

Now let’s write a "Hello World…" function.

A "Hello World" function
————————-

Now let’s create a real plugin; the file is called "helloworld.php", which will go in the "plugins" directory. The function is plain php; the "Plugin Name" entry in the commented section is is mandatory.

< ?php
/*
Plugin Name: Hello World
*/
function helloworld() {
echo "Hello World…";
}

Now if you would add the closing php-tag, you could see it already in the plugins part of the site administration. Right now, it is not a plugin yet, because it merely defines a function. The next step is to put the function on the hook. Keep the editor open!

Attaching a function to a hook
——————————

Just now we did two things:

  1. we created a hook somewhere in the generation of the page, and
  2. we created a function that is to be called when the hook is reached.

The next step is to attach the function to the hook. This is done by calling the function add_action, where its first parameter is the name of the hook (‘lv_content_head’ in our case), and its second parameter is the name of the function (‘helloworld’ in our case).

Our editor is still waiting for us, so add the function to the hook, and close the php part:

add_action("lv_content_head", "helloworld");
?>

Now the entire plugin looks like this:

< ?php
/*
Plugin Name: Hello World
*/
function helloworld() {
echo "Hello World…";
}

add_action("lv_content_head", "helloworld");

?>

Call it helloworld.php and put it in the wp-content/plugins directory.

From the admin panel it can now be activated, and it should display "Hello World…" on the top of the content area.

A few more pictures from Canada

I finally managed to pry a few more pictures off of my cellphone. This is Kiko, Chris, Franz & I celebrating Chris B’s birthday at a very good restaurant: Banzai sushi. I still owe him a present though… (not that seeing me wasn’t present enough mind you…) ps. Franz was kind of enough to treat Kiko and I to dinner. Thank you!! (my brain was not working, and I apologize, but upon reflection I could have used that as an opportunity to give Chris a birthday present and pay for his dinner. d’oh. chris, forgive me!)

 

chrisfranzdavidcanada2005.jpg   davekikojuly2005.jpg

The Sunflower Parade

I thought this post was going to be more exciting… (as you can see we have the first ever baby name poll happening to your right, SO VOTE!) but things have been pretty hectic. emoticon

We are finally getting over our jetlag, and even though my school visits are over until September I am preparing for the arrival of the new JET‘s to Japan as a member of the Tokyo Orientation Committee and the Kagawa Orientation.

This Friday I also have a Jamaican Cooking day in my town. Apparently I am in charge so I am going to try and make something resembling Jamaican cooking. They also are trying to get me to sing some Bob Marley… umm… no.

Other than that, I am reading Harry Potter 6 (yeah!) and am getting ready for a busy summer:

  1. July 30th to August 3rd: Tokyo Orientation (in Tokyo)
  2. August 5th: Kagawa Orientation
  3. August (dunno): Interning at the Canadian Embassy in Tokyo (yeah!)
  4. August 25th: Kagawa Orientation #2
  5. Rest of my life: Baby.

My town is known for sunflowers (and frogs, and rice, and being a really small town) and their festival was actually a lot crazier than I thought it would be. Take a look! (click on the picture to go to a gallery.)

2005 Sunflower

Here is a picture of me being a Japanese ninja archer from like 2 years ago. I ran into it on my computer a few days ago. I’m cool. 

I am a Japanese Ninja

Summer in Canada 2005

We are  back in Chunan!!! … and have begun the process to get rid of as much jetlag as possible. We both had a fantastic time back in Canada.

I had a scholarship interview that went fantastic emoticon(I’m gonna need something to do when my contract here ends in 2006/08 or 2007/08.) and we got to see a number of really good friends. That being said, we were completely strapped for time, and for everyone that we were able to see, there were 2 more people we would have loved to but couldn’t. Between my scholarship interview in the first week, my Dad’s operation in the second (he’s ok!) and the fact that we only had 10 days in Canada, we did our best.

For those that we did see: Thank you!!! We had an amazing time. emoticonFor those we didn’t: Next year, February. emoticonWe miss you all, and unfortunately because of the baby, this will be our only trip to Canada back this year. Next year we will be back with Baby Awesome (or whatever he/she is named at the time.)

In the meantime, here are a few sample pictures of what we did while back in Canada, and the full gallery can be seen by clicking here.

Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture   Canada 2005 Picture

On Our Way

yo.

Just a quick note… we are on our way! I am writing this at 5:32am and we have to be at the airport by 6. I have a sore throat, Kiko is large, and our suitcases are super heavy.

We will be posting a lot about our time in Canada, pictures, some amazing times seeing good friends and family, pictures of my good friend Mike and his new baby, and lots of other excitement. In the meantime, we have a 20+hr transport looming ahead of us, so wish us luck.

We will be missing everyone in Canada terribly, but I have to get back and water the watermelons… or something.

Look for pictures soon…

love david & kiko.

The Big News

Why is Kiko so large-ish in the previous entry???emoticon

Drumroll….

   Both Kiko and I would obviously like to be able to give everyone the good news face to face, but since that really isn’t much of an option, we want to take this opportunity to let all of our good friends and family (and the stragglers from the internet who accidently find themselves here… i guess) know the good news…
Introducing David Jr. aka Kunio aka We don’t have a name yet! emoticon

 

Now accepting name submissions. There will be a vote. You can forever improve or mess-up it’s life. Make it so.

 

davidjr.jpg 

 

 

Kiko & David! & Off to Toronto

Just a quick note before we set off for Toronto. It will be a very short and action packed filled trip as we head back to Toronto for 10 days. I have a big scholarship interview but after that is done (30th) I hope to see as many people as I can in the last 4 days. Wish me luck!

See you guys soon and wish us a safe flight! …

Kiko’s New T-Shirt 

No matter what she tells you, this is Kiko’s new favourite shirt. (you can tell by how happy she looks in it!!) 

Another big thanks to Chris and Lianne! Everything you guys have sent has been amazing…. miss and love you guys.  emoticon 

kiko-and-david!1.jpg 

 

 

I Love… Dokdo?

Time to give you a little insight into the region where Kiko and I are living.

Japan wasn’t exactly the most popular country right after World War II. After invading the rest of Asia (to save it from the whiteys… who else?) many of the regions they had taken over were being claimed by various countries. (Mostly China, Korea etc…)

Fast forward to 2005. There is this tiny little ass island called Takeshima in Japanese, and Dokdo in Korean. Now Japan (The Country) claims it as their own island, while Korea claims it as theirs. For some unknown reason Korea got a little excited about this and made a big deal about it. After 3 Korean people cutting their fingers off, 40 Korean’s moving to the island, Korean TV Shows & Movies about Imperial Japan, and 130million Japanese people going "… we have an island called Takeshima?" emoticon Read, learn, and become smarter here.

edited: Wow. After a little searching I found this. Koreans seem pretty excited about these rocks. Welcome to Cyber Dokdo.

I am proud to present my "I love Dokdo" shirt from my very good friends Chris and Lianne in Korea.

I wear it around Japan, but they just laugh. They cannot fathom why there are "I love Dokdo" goods selling in Korea, and the average Japanese person would be more than happy to give it back…

 

dokdo.jpg 

 … and yes those are I love Dokdo socks and a pencil case. 

Vegetable Update

I bet you were hoping for something exciting… right? Too bad. I’ve been getting a lot of flak about being domesticated. What the hell does that mean? Well if it means "trapped in the middle of nowhere, surrounded by only farmland and rice paddies, having nothing else to do but make sweet sweet vegetables that you are all secertly jealous of" then yes… yes I am domesticated. As soon as I get the update to the website finished, you should see a lot of changes and (in theory) the website should be a lot more fun, and easier for me to update. Cuz’ I’m lazy. The keeners out there would note that the photo albums are being updated fairly regularily (go check them out!) and I have added a stats page and a poll booth. All in preperation for the new design! We also have a ton of news coming (from both Kiko and I) as well as a trip back to Canada this month. So stay tuned… in the meantime, bow down to my potatoes (and my new farming boots!), my tomatoes and my watermelon (which has now sprung to 7 watermelons!!!) (note the HUGE pictures)

our potatoes

 

our potatoes

 

our tomatoes

 

our watermelons