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).
Thesis Hooks
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):
- thesis_hook_before_post
- thesis_hook_after_post
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:
function hello_world(){
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:
add_action(‘thesis_hook_before_post’, ‘hello_world’);
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:
add_action(‘thesis_hook_before_post’,’social_welcome’);
add_action(‘thesis_hook_after_post’,’social_friendship’);
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. 🙂
Just bought Thesis for a few days and still looking around for a useful hints on hooks and functions for a newbie cum non-technical guy like me.
Thankfully I’ve managed to discover your site, DRAGOS. A simple yet meaningful guidance for me to start loving my thesis and make it worth every penny I’ve spent!
The download link takes me to a 404.
.-= Vivek´s last blog ..Airtel and the proof of address =-.
Dragos,
Good article – thanks for taking the time to lay this out for us. I’m clicking on the link to download the functions code and getting a 404. Could be a problem on my end, but just thought I’d check and see if maybe something is up on your end.
How do we write code to incorporate Twitter along with stumbleupon, digg and reddit? Thanks for the help.
the URL for Twitter should be something like this:
…http://twitter.com/home?status=Currently reading “. get_permalink(); ?>.”
I’ll update the code in a few days to support it too. The problem is that a lot fo Twitter traffic comes from URL shorteners so we can’t correctly identify the referrer all the time. I think less than 5% of links from Twitter are not handled by URL shorteners and keeping a list of those shorteners and verify against them, all in a thesis hook is cumbersome.
Thanks, dragos, for this post!
I tried a “Referrer” plugin to try to accomplish this but it kept interfering with other plugins on my site. Your expertise was the answer to my dilemma!
Patrick MacNamara’s last blog post..How to Setup a Blog for Your Chiropractic Practice
@Jonathan the good things about the hooks is your total control. There are some plugins that simply can’t be replaced by hooks, but overall I can do whatever I want with hooks. So far 🙂
I am also a code dummy like Jay. I have worked with Thesis hooks some, and so far everything is working. I still tend to use plugins when available just to make it easier but your walk through was very helpful. I had noticed the different greetings depending on where I came to your blog from, and I wondered how you did that. I use a plugin called “what would seth do” but it lacks the versatility that hooks offer.
Jonathan – Advanced Life Skills’s last blog post..When Your Inner Voice Speaks Do You Listen?
@Jay glad you find any value in that 🙂
@Fatos using the same hook ‘thesis_hook_after_post’ but a different styling. The content for the box is easy to echo in a function, I suppose, and then you hook it with a call to add_action() like this:
add_action(‘thesis_hook_after_post’, ‘my_function’);
here’s the styling I use, feel free to adapt it to your layout/design:
div style=”background:#efefef;border: 1px solid #ccc; padding-left: 4px; float: right; margin-left: 12px; width=200px;”
hope this helps 🙂
by the way, how did you add that bookmark and share box on the right hand side under the post? I wold really appreaciate if you could tell me.
I love thesis but I am a coding dummy so I am afraid to touch it- this is making me less afraid. I am bookmarking this now. You are just full of good info man- thanks!
Jay’s last blog post..How to Save the World
@Sid Savara yeap, I know the feeling, sometimes I wish I have total freedom and change my blog whenever I want. Hooks are a reasonable compromise for somebody familiar with PHP because they eliminate the need for a large number of plugins and give you significantly more control. A lot of plugins are just echoing stuff for you based on certain conditions and hooks are just great for that.
@Toma feel free to do it the way you like it, you know what they say: the shortes path is the one you know. Thanks for your comment 🙂
Hi Dragos,
Great article. Those hooks sound really great. I have to manual place the code where I want it to appear. I really like the tag line just on the beginning of your article reminding the reader to subscribe : I’ll implement something like that on my blog too.
Thanks,
@TomaBonciu on Twitter
Toma – Optimizing The Web’s last blog post..How do you develop a great community that will help you achieve your goals ?
I think your tutorial is spot on, well written and very useful/
Interestingly, hooks is one of those things I’m on the fence out about Thesis. I like that the flexibilty for customization is there – but I specifically *don’t* like the fact that other themes don’t do it, so if I put in bunch of hooks (rather than customizing it through a plugin control panel), I’m “locked in.”
I know that the purpose is to provide flexibility, and hooks are much better than modifying theme files. It just seems like an in between shortcut (e.g., I don’t want to modify the theme, but I don’t want to write/install a plugin either – so I’ll use a hook)