Thesis Hooks for Twitter, StumbleUpon, Digg and Reddit

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:

Thesis Hooks For Social Networks (2002)

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. 🙂



Running For My Life - from zero to ultramarathoner


The spooky thing about depression is that it sneaks in. There aren’t really trumpets and loud voices announcing: “Hail, hail, this is depression entering the room, all rise!” Nope. It’s slow, silent, creepy. It doesn’t even look like depression. It starts with small isolation thoughts like: “Maybe I shouldn’t get out today, I just don’t feel like going out”. And then it does the same next day. And then the day after that and so on. And then it starts to whisper louder and louder in your ears: “Why would you go outside, you loser? Didn’t have enough yet? Want more people to make fun of how much of a big, fat loser you are?”

And then you start to breath in guilt and shame, instead of air. Every breathe you take is putting more dark thoughts into your body.

Until you get stuck. You can’t move anymore. At all.

If you want to know how I got out of this space, eventually, check out my latest book on Amazon and Kindle.

Running For My Life -from zero to ultramarathoner

Dragos Roua

The guy who started all this. Entrepreneur, ultra-marathoner, tanguero, father and risk taker. I'm blogging here, but I also spend a lot of time in this marvelous space.. You're invited, by the way.

This Post Has 25 Comments

  1. 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)

  2. 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 ?

  3. @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 🙂

  4. 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

  5. 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.

  6. @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 🙂

  7. […] Thesis Hooks Tutorial for Twitter, StumbleUpon, Digg and Reddit Simple hooks for displaying welcome messages to your visitors from social networks like Twitter, StumbleUpon, Digg, Reddit. (tags: thesis thesiswp tutorials wordpress bookmarking socialmedia howto hooks digg twitter stumbleupon reddit) […]

  8. 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?

  9. @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 🙂

    1. 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.

  10. […] Simple Thesis Hooks for Twitter, StumbleUpon, Digg and Reddit – Dragous Roua […]

  11. 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.

  12. […] Simple Thesis Hooks for Twitter, StumbleUpon, Digg and Reddit […]

  13. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

What is 5 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)