Nerd tech, entertainment, and web development blog

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.

Written: Dec 10, 2007


Related posts »

8 Responses to "WP Plugin: Turning ‘Popularity Contest’ into a Widget"

  • Rudy
    December 10, 2007 @ 12:31 pm


    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?

    • Gary R. Hess
      December 10, 2007 @ 4:49 pm


      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.

  • Rudy
    December 13, 2007 @ 8:34 am


    Thanks for your insight, Gary.

  • Rudy
    December 13, 2007 @ 8:36 am


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

  • Rudy
    December 13, 2007 @ 8:40 am


    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!

    • Gary R. Hess
      December 13, 2007 @ 10:26 am


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

  • PzheL
    November 27, 2008 @ 7:56 am


    I got my eyes crazy but did not manage to get the widget work.

  • pranajayas
    March 12, 2009 @ 5:50 am


    good info about wp plugins, I will bookmarking this site thanks