WP Plugin: Turning ‘Popularity Contest’ into a Widget

WordPress has tons of great features, one of them is being able to easily interchange widgets on the fly.

A popular WP plugin is Popularity Contest by Alex King. The plugin allows the admin to show the most popular pages over a certain period of time (a month, year or all time). The admin is able to change the requirements of making a page popular by using the point system provided within the plugin.

Being such a great plugin, I thought it was a disappointment that it is not supported as a widget. After doing some research, I was able to make it possible.

So how did I do it? By adding this to the end of the popularity-contest.php file within the plugins directory:

function widget_popcontest_init() {

if (!function_exists(’register_sidebar_widget’)) {
return;
}
function widget_popcontest($args) {
extract($args);
echo $before_widget . $before_title . ‘Popular Articles’ . $after_title . ‘<ul>’;
if(!$options = get_option(’akpc_most_popular’)) $options = array(’limit’=>10, ‘before’=>’<li>’, ‘after’=>’</li>’);
akpc_most_popular($limit = $options['limit'], $before = $options['before'], $after = $options['after']);
echo ‘</ul>’ . $after_widget;
}

function widget_popcontest_options() {

if(!$options = get_option(’akpc_most_popular’)) $options = array(’limit’=>10, ‘before’=>’<li>’,'after’=>’</li>’);
if($_POST['popcontest-submit']) {

$options = array(’limit’ => $_POST['popcontest-limit'], ‘before’ => $_POST['popcontest-before'], ‘after’ => $_POST['popcontest-after']);
update_option(’akpc_most_popular’, $options);

}
echo ‘<p>Limit: <input type=”text” name=”popcontest-limit” value=”‘.$options['limit'].’” id=”popcontest-limit” /></p>’;
echo ‘<p>Before: <input type=”text” name=”popcontest-before” value=”‘.$options['before'].’” id=”popcontest-before” /></p>’;

echo ‘<p>After: <input type=”text” name=”popcontest-after” value=”‘.$options['after'].’” id=”popcontest-after” /></p>’;

echo ‘<input type=”hidden” id=”popcontest-submit” name=”popcontest-submit” value=”1″ />’;

}
register_sidebar_widget(’Popularity Contest’,'widget_popcontest’);
register_widget_control(’Popularity Contest’,'widget_popcontest_options’, 200, 200);
}

add_action(’plugins_loaded’, ‘widget_popcontest_init’);

Although this only works for the most popular of all time, it can easily be changed to use the other great functions.

Note: if you are wanting to use this, you must change all quote marks to regular quotes within a text editor program like notepad.

Add to Del.cio.us RSS Feed Add to Technorati Favorites Stumble It!
   www.sajithmr.com

Rate:
2.8

If you enjoyed this post, please consider subscribing to my full feed RSS.

Written: Dec 10, 2007
Tags: , , ,

Related Articles:
2007: Year in Review
Winner of the EntreCard Contest
John Chow’s Million Dollar Wiki Blog Contest
Entrecard Contest: 1000 Free Credits
Featured Posts Plugin for WordPress


RSS feed

6 Comments »

Comment by no imageRudy (Who am I?)
2007-12-10 12:31:01

Good info.

I’ve emailed Alex about getting AKPC to do popular post for the past 60 or 90 days (or any arbitrary # of days). No response yet. Do you know if that’s possible?

Rate:
2.8
Comment by no imageGary R. Hess (Who am I?)
2007-12-10 16:49:46

It is absolutely possible, but not without re-writing some of the original code. As far as I can tell the Popularity Contest Options page within the admin section has its own set of functions only available from within that specific page–meaning it is not possible to use the 60 or 90 days within the visible blog area of your readers.

I have been working on a separate solution by grabbing the DB from a new file and then displaying the list from there, but that could get time consuming and would probably be easier to just rewrite the original script.

I wish there was an easier solution, but with the way the point system is setup it really can’t get there.

I’m not really a fan of how WordPress connects the plugins anyhow, hopefully Alex can give a simpler solution to the problem.

Rate:
2.9
 
 
Comment by no imageRudy (Who am I?)
2007-12-13 08:34:37

Thanks for your insight, Gary.

Rate:
2.8
 
Comment by no imageRudy (Who am I?)
2007-12-13 08:36:25

Thanks for your insight, Gary. I guess I’ll have to make do with whatever we have so far.

Rate:
2.8
 
Comment by no imageRudy (Who am I?)
2007-12-13 08:40:41

Oops, I didn’t mean to post so many times. It looked like the reply within a reply plugin was experiencing a problem. First the error said, “I already said that”, and then the 2nd error was a blank page. But both posts above made it.

Hmm!

Rate:
2.8
Comment by no imageGary R. Hess (Who am I?)
2007-12-13 10:26:01

Yeah it does that sometimes, not exactly sure on the issue just yet.

Rate:
2.9
 
 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.
Advertise on impNERD.com
Elite By Design WordPress Theme
1993 Honda Civic
Make money online
PoemofQuotes Blog