ArticlesForumDownload AboutContact

boakes.org

nice of you to drop by. tea?

Tags: Releases, WordPress

MostWanted - a Popular Posts Plugin for WordPress

November 24th, 2004, by Rich.


Warning: apache_lookup_uri() [function.apache-lookup-uri]: Unable to include '/pics/2005/mostwanted/mostwanted' - error finding URI in /home/www/boakes.org/htdocs/mods/plugins/boakes-depicticon.php on line 65
MostWanted - a Popular Posts Plugin for WordPress

This WordPress Plugin which lists the most popular posts in a blog, according to the records held by StatTraq

MostWanted lists the most popular posts on a wordpress powered weblog. This list can be used in the sidebar to provided visitors with an indication of what are the most visited pages.

The MostWanted plugin in use.It’s grown a little since it’s original release so it has a subtle API change. Where previously it was called “rjb_mostwanted”, it is now “MostWanted::mostwanted”. Currently there is a wrapper around the old method name so that it still works, however this will be removed in future releases.

Installation

  1. Download this fileDownload this file
  2. Rename it mostwanted.php and copy it to your /wp-content/plugins folder.
  3. using wp-admin, enable the plugin

Usage

The plugin provides one method of interest: MostWanted::mostwanted($top_n, $trim_chars, $showviews). The three parameters are:

  1. $top_n
    the number of results to list default = 5
  2. $curtail
    0 for no text curtailment, or ‘n’ the number of characters from each post title that shoudl be displayed. e.g. Curtailing “My Dynamic Badger” to 10 characters would read “My Dyna…”.
    default = no curtailment
  3. $showviews
    true if the number of times each post has been viewed should be included in the list.
    default = false
  4. $show_views_in_tt
    TT is short for ToolTip setting this value to true includes the number of views as part of the tool tip. i.e. if you hover over the text the (1234 distinct viewers) message is shown.
    default = true
  5. $duration
    restricts the duration of the query period so that only the last $duration days are considered when measuring popularity. e.g. a value of 30 would return the number of users only within the last 30 days. Leaving the value unset, or 0, results in the all-time results being returned.
    default = 0 (all-time)
  6. $pre
    this is a text string that is added to every line.
    default = <li>
  7. $post
    this is a text string that is added to every line.
    default = </li>
  8. $method
    there are two ways of identifying unique visitors, neither of which are perfect, the default is to recognize only unique IP addresses, which means that if several people from one company visit, then they may show as a single user. Alternatively, using the session_id is not perfect because some users refuse to set cookies.
    default = ip, alternative = session
  9. $as_percentage
    if set to true, then the number of hits for each page as a percentage of the site total is displayed instead of the hit count itself. This may be desirable if you want to show popularity without letting on how many hits you get for each story.
    default = false
  10. $timeout
    adjusts how long the most-wanted information is cached for before being replenished from the db. A value of 0 (zero) will result in fresh data being retrieved for every query, whereas a value of 3600 will only query the database once every hour, cacheing the output and thus reducing DB load.
    default = 1800

The simplest way to use the plugin is therefore to augment your page with:
<ul>
<?php MostWanted::mostwanted(); ?>
</ul>

A more tuned version might read:
<ul>
<?php MostWanted::mostwanted(7, 30, true); ?>
</ul>

License

MostWanted is released under a Creative Commons License.

Credits

If you find MostWanted useful, please feel free to link or a trackback to this entry.
Thanks to everyone whose commented with problems, solutions & suggestions, especially:

207 Responses to “MostWanted - a Popular Posts Plugin for WordPress”

Pages: «134567891011121321»

  1. 71
    hansen Says:

    hehe ,i am using the last version of stattraq,but i have found that the “counter number” in the sidebar is different from the “user hit” in the /wp-srattraq/index.php,so i want the sidebar shows the “user hit” in the sidebar! what can i do for it?
    so sorry that my spelling english is not so good!i come from chinese.

  2. 72
    rich boakes Says:

    Hi Hansen, the mostwanted function allows you to enter a method for how things are counted. The default method is IP address, however, you can also use session ID’s, which should give you the same numbers you see in stattraq.

    Both have shortcomings:

    • IP address falls down when you have large groups of people coming through the same router (if it uses Network Address Translation (NAT)) because they all appear to have the same IP. For example, when my wife or her office buddy visit the site, they both appear to have the same IP address, so viewer numbers appear lower than they really are. In use, it looks like this:MostWanted::mostwanted(, , , , , , , "ip");
    • SessionID fails because some users don’t allow cookies to be set, so every page they visit gets a new session ID. The result is that user numbers appear higher than they really are. MostWanted::mostwanted(, , , , , , , "session_id");

    So, to switch to session_id based counting, set the eighth parameter to “session_id”.

    There is no ultimate solution to this, you just have to understand the issues and account for them. If you have any problems post your current function call and I’ll take look.

  3. 73
    hansen Says:

    thanks a lot for your reply,and you give me a lot of knowledge that i really do not know!
    and i will do it as you suggested.

  4. 74
    Geekess Says:

    I can’t get the Mostwanted plugin to work with my site.
    I’m using Wordpress 1.5.1.3, stattraq and mostwanted (both the latest versions). They are enabled in the pluginscreen. Stattraq works, but Mostwanted doesn’t register any statistics. It just says: No results available.
    I have installed it yesterday morning, and I had some people click the links to generate some statistics.
    Any ideas?

  5. 75
    rich boakes Says:

    Hi Geekess,
    you’re seeing an artefact caused by stattraq not handling permalinks 100% perfectly. There are assorted minor hacks that vary based on permalink structure. Michelle Li posted her solution here a while back, and having taken a quick look at your site it looks like the code will fit the bill for you.

    You can also knock your existing records into shape using the MostWanted::fixstats() function, which assigns the correct postid in cases where it’s missing (which is what happens when permalinks are used).

    To use it, create a file called fixstats.php and to that file add:

    <?php MostWanted::fixstats(); ?>

    Depending on the size of your database it will take between a jiffy and an age to complete.

  6. 76
    Geekess Says:

    Thank you, it’s fixed! (for now).

  7. 77
    rich boakes Says:

    Great, Randy has said the next version of StatTraq will support permalinks by default, so such shenanigens shouldn’t be necessary any more.

  8. 78
    Steve Says:

    Morning Rich,

    If I include Michelle Li’s fix (linked to above?) will it sort out my
    >> No results available problem?

    I installed Stattraq and your MostWanted plug-in last night, but have not been able to use it to display on my site.

    In which file does Michelle’s fix need to go?

    Cheers,

    Steve

  9. 79
    rich boakes Says:

    Hi Steve, short answer, yes, these are the droids you’re looking for.

    The longer answer is that the code needs to go in stattraq.php which is in your plugins directory. The suitability all depends on the structure you use for your permalinks. Personally I don’t use dates in my URI’s because I prefer to make each story name unique, this gives me shorter links, which readers tend to prefer.

    For reference, my stat_traq_event function, which only works for “dateless permalinks”, looks like this - you’ll (of course) note that my SQL statement includes the delayed directive, that makes stattraq much faster:

    [php]function stat_traq_event($passed_param){
    global $doing_rss, $p, $post, $tablestattraq, $tableposts, $wpdb, $browser_type;
    $wpdb->hide_errors();
    $s_id = session_id();

    // need to get the real article_id or type of server request (RSS, RDF, ATOM, Ping, etc)
    if(!isset($article_id)){
    //if (($p != ”)){
    // $p = intval($p);

    $article_id = 0; // default/mixed page - not just for one article

    if (($post->ID != ”)){
    $article_id = intval($post->ID);
    }else if(isset($doing_rss) && $doing_rss == 1){
    $article_id = “Feed”;
    }else{
    if (isset($_SERVER['QUERY_STRING'])) {
    parse_str( $_SERVER['QUERY_STRING'] );
    if (isset($name)) {
    $query= ’select id from wp_posts where post_name = “‘.$name.’”‘;
    $p = $wpdb->get_var($query);
    if ($p!=”) { $article_id=$p; }
    }
    }
    }
    }

    if(!isset($s_id)){
    $s_id = session_id();
    }

    $urlRequested = $_SERVER['PHP_SELF'] . (isset($_SERVER['QUERY_STRING']) ? “?”.$_SERVER['QUERY_STRING'] : ” );
    $ipAddress = statTraqGetIPAddress();
    $browser = statTraqGetBrowser();
    $referrer = (isset($_SERVER['HTTP_REFERER']) ? “‘” . $_SERVER['HTTP_REFERER'] . “‘” :”NULL”);
    $userAgent = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : “NULL”);
    $search_phrase = statTraqGetSearchPhrase($referrer);
    $spamFlag = statTraqGetSpamFlag($referrer);

    if (!strstr($_SERVER['PHP_SELF'], ‘wp-admin’) && !strstr($_SERVER['PHP_SELF'], ‘wp-stattraq’)) {
    $wpdb->query(”INSERT delayed INTO $tablestattraq (session_id, access_time, ip_address, url, article_id, referrer, user_agent, browser, user_agent_type, spam, search_phrase) values (’$s_id’, NOW(), ‘$ipAddress’,'$urlRequested’, ‘$article_id’, $referrer,’$userAgent’,'$browser’, ‘$browser_type’, $spamFlag, “. ($search_phrase==null?”NULL” : “‘$search_phrase’”) . “)”);
    }
    $wpdb->show_errors();
    return $passed_param;
    }[/php]

  10. 80
    Trackback from: I’m not finished yet » Blog Archive » Site stats

Pages: «134567891011121321»