How to add Sphere It function based on number of words in post |
Techcrunch points out that Time has started using the Sphere It function on its articles. I took a look at the functionality and liked the results it returned. Note: Sphere beta was reviewed at Hmm in November 2005.
The Sphere It function analyzes the context of a page and offers related post results from other blogs. I checked out a few posts at Hmm to see if Sphere It was returning relevant results and decided this could be useful in the share/bookmark link section at the end of each post here for readers. I wanted to implement this.
However, I did not want this Sphere It link to show up on posts without a certain (configurable) amount of words, so I create a small hack for the WP single.php template to check the number of words in the post and if the criteria was met (more than 75 words) return the Sphere It link with the proper permalink for that post. The code would also strip out the HTML tags so those weren’t counted in the total number of words for each post.
Working examples
See this post which contains more than 75 words. Note the “save/share/explore” link with Sphere it afterwards. All other posts show “save/share” only after the post and the Sphere it link is not there. See example with less than 75 words here.
Instructions
Though the following how to instructions are intended for Wordpress server-side powered blogs (not wordpress.com) it is possible to use a similar technique in other blogs that allow PHP scripting.
Edit the single.php template (found in admin area under Presentation->Theme Editor->Single Post) by replacing the following code:
with:
comments_popup_link(__(‘Comments (0)’), __(‘Comments (1)’), __(‘Comments (%)’));
// wordcount hack by TDavid
$your_content = strip_tags($post->post_content);
$your_num_words = str_word_count($your_content);
if($your_num_words > 75) {
?>
<a href=“http://www.sphere.com/search?q=sphereit:<?php urlencode(the_permalink()) ?>”>Sphere it!</a>
< ?php
} // end optional show sphere it for posts with more than 75 words
?>
STEP 2. Save the changes by clicking on “update” and then visit a post with more than 75 words and one with less to test. All well, no errors? You’re done.
Notes
This code would be neater wrapped in a plugin, but I wanted something quick and dirty so I could update from within the template. Feel free to make a plugin out of this if you want.
Did this post make you go hmm?
Maybe Related Posts (plugin generated)
- How to add a dynamic digg vote this button, show related posts and more using Wordpress
- [site news] Average post length numbers
- How to identify and remove Wordpress comment spam MySQL queries
- Disabling Nofollow in Wordpress 1.5
- How to build a Google subscribed link for your about/bio page step by step
- First detailed look at Sphere beta




Nice little hack. Thanks for sharing.
Comment by Sterling Camden — May 23, 2006 @ 4:08 pm PST
YW Sterling, thanks for stopping by as always
Comment by TDavid — May 23, 2006 @ 4:39 pm PST
[…] TDavid gives us the PHP code to add a “Sphere it” link to the end of your WordPress posts if they contain a minimum word count. Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages. […]
Pingback by Searching around with Sphere -- Chip’s Quips — May 26, 2006 @ 7:48 pm PST
[…] The most impressive blog search I’ve seen to date has been the Sphere It tool mentioned last week which does a pretty darn good job figuring out what the blog post is about and showing new blog posts. In fact, you can try it out by clicking the Sphere It link at the end of this post. […]
Pingback by Make You Go Hmm: » So blog vain, you probably think this new Ask search is about you — June 1, 2006 @ 12:41 pm PST
[…] Why is it that How to add Sphere It function based on number of words in post is the closest I seem to get to a simple wordpress plugin that adds a “sphere it” link to the bottom of each post? […]
Pingback by Sphere It Plugin for WordPress | Routine Order — January 2, 2007 @ 2:48 pm PST