I love my wordpress theme, I really do. In case you haven’t noticed so far, this is thesis, one of the best commercial themes for wordpress. I don’t like it only for the crisp appearance and nice layout, you can get that nowadays from any decent free wordpress theme. What I like it for is the unparalleled flexibility of the backend. Not only it has 2 different admin pages full of options but it comes with one of the best features in the PHP frameworks: hooks.
Now it’s time to take a break and announce to our precious reader that this post will be a moderately technical one (yes, I know PHP pretty well, among other different skills ) and it will involve some coding. Now, let’s explain what this is all about.
Take Care Of Your Users
I use several social networking sites these days: StumbleUpon, Digg, Reddit and of course, Twitter. During the last few months I managed to attract a fairly amount of traffic from those services. To be honest, this traffic exceeds by far anything I get from search engines. And what I really appreciate at this type of traffic is its “human” touch. I might get some huge impact from a lot of bots that are searching for “iPhone 3g applications” (btw: I rank pretty well for that) but in the end what counts are the real persons who are seeing my posts promoted on the social networking services.
So, I thought it would be courteous for me to let those people know that I know from where they are coming and to start a little bit of interaction with them. I decided to have this interaction on two very hot spots in the wordpress territory: just before the post and just after the post. Sidebars, header and footer are not so hot when it comes to user action, the content rules. So, I wanted to have a simple line that would say hi to the visitor and tell him I’m on that specific social network too and a little reminder to help him promote the post, and another simple line in the end, letting him know how he can befriend me on those networks.
Pretty simple, of course. I know you can get tons of social networking features packed in a number of very specialized plugins but I didn’t wanted to use another plugin for that. I wanted to keep it extremely simple and easy to control. So, here’s how I did it (don’t worry now for the technical part, all the source code is packed and ready to download at the end of the post).
Yes, let’s talk a little about those hooks. In simple terms, a hook is a place where you can interfere with a certain surface of an online application. I’m quite familiar with hooks since I’ve extensively used a PHP framework called Code Igniter, which already had this feature. So, in even simpler words, if you want to display something in your thesis theme, you can do it with maximum flexibility. You can put whatever you want whenever you want. And you can do it without compromising thesis file structure.
In order to work with those hooks you have to write some code in a file called custom_functions.php, located in the custom folder of your thesis installation (and that would be a folder called exactly like this: “custom”). After you decided what you want to display – in our case a greeting and some interaction invitations – and where – in our case just before the post and just after the post – all you have to do is to browse through the Thesis Hook Reference List to find the places. My hooks were named (quite obvious, I know):
Now it’s time to start some coding.
Modifying Your Blog With Thesis Hooks
The hook mechanism is very simple: you put your data in a separate function. Let’s say you want to display the immortal “Hello World” just before your post. Write – in custom_functions.php, of course – a function called hello_world(). The function should look like this:
echo “Hello world!”;
You have your function, right. Now, in order to hook it with the main thesis framework you’ll use a function called add_action(); The first argument is the hook name and the second is the name of your function. So, if you want to add the “hello world” text to the place just before the post, your add_action() call would look just like this:
Save custom_functions.php, fire up your browser and voila: your text is there. I highly recommend to remove this test fairly quickly, as fascinating as it might be for yourself it might become frustrating for your users :-).
But enough saying, I’m sure you got the idea. Let’s quickly described how I implemented my hooks for social networking sites. First, I did some basic referer checking (the page from where the user is browsing my blog). If there is no referer (user is coming from a bookmark, for instance) I just display a simple greeting with a feed subscription invitation. If there is no referer I don’t show nothing after the post either. If the referer is one of stumbleupon.com, digg.com or reddit.com, I echo my message. And after the post I also show my friendship invitation with a link to my social profile.
I created two functions: social_welcome() and social_friendship(). Each function take care of intercepting the referer and echoing the right message (you’ll have all the source code in the file). After the function declarations I hooked them up by using the following add_action() calls:
That’s all. Oh, you must download the file from here: [download#10] All you have to do is to customize the text for the messages – read the inline comments in the file for that – and then copy / paste the content into your custom_functions.php file on your custom thesis folder (again, a folder which is called “custom”).
Later edit: unfortunately, the source code is no longer available, but, hopefully, the indications should be enough to get you started to build your own.
Now that’s really all. 🙂
Narrowing The Game
If you’re not yet convinced why you should try this, here are some advantages:
- no need to write or install another plugin
- total control of the styling
- total control of the text
- put as many social services as you want
- being in custom_functions.php file assures onward compatibility with future versions of thesis.
Oh, you don’t have thesis yet? Sorry to hear about that, but there’s still time, go grab your copy here. I already did. 🙂