DokuWiki

It's better when it's simple

User Tools

Site Tools


Sidebar

Translations of this page?:

Learn about DokuWiki

Advanced Use

Corporate Use

Our Community


Follow us on Facebook, Twitter and other social networks.

tips:viewcounter

View Counter

This is not a pure plugin for DokuWiki - manual code modifications are needed.

More information and installation instruction

Solution using meta data

This view counter is a good idea, but the code is a little bit outdated. Nowadays we would use page meta data to store a view counter and don't store it in an proprietary file structure.

I found a plug-in too complicated and so I extended my current template to count page views and show the number in the footer the same way Alexander did in his code. To do this you only need to add following function to your template code:

function tpl_newpageinfo(){
    global $lang;
    global $ID;
 
    $pinfo = tpl_pageinfo(true);
    if ($pinfo === false) return false;
 
    $viewcnt = p_get_metadata($ID, "viewcnt");
    if ($viewcnt == null) $viewcnt = 0;
    $viewcnt++;
    p_set_metadata($ID, array('viewcnt' => $viewcnt));
 
    $pinfo = str_replace(' · ', ' ('.$viewcnt.' '.$lang['views'].') · ', $pinfo);
    echo $pinfo;
}

Then replace the call to tpl_pageinfo() with a call to the new function tpl_newpageinfo() and that's it.

“What does this (sentence above) mean, can you explain a little?”Ivan
The tpl_pageinfo() function is found in inc/template.php. You then change all calls to tpl_pageinfo() to tpl_newpageinfo() which calls tpl_pageinfo() anyway, so it's just a way to patch the existing function. There's another example of how to get a counter here: http://wiki.erazor-zone.de/wiki:projects:php:dokuwiki:counter - Aaron

I added the word “views”, used in the footer, to the template translation file. If you don't need or like this, you may remove the parts with $lang from the function. Otherwise you have to add an entry like this to your template translation files:

  $lang['views']     = 'views';

Matthias Grimm 2009/08/22 12:47

Note : The word “views” don't show for me. I replaced $lang['views'] by tpl_getLang('views') in your code and it works :)

It does not work with dokuwiki-2012-09-10. -Anon

Seriously can we not just make this a plug-in? I already messed up my Wiki by trying to edit manually, let's make this easier for the rest of us please -ofcourse

Plug-in would be nice but there is one problem concerning where to show this information. If we want it to show in same place where “Last modified”-text is shown there is no way to avoid editing template file. As far as I know. Otherwise I have managed to make Action Pluging which counts page views, but my problem is data presenting because I don't know how to edit xhtml code using current set of events. - SomeCoder

It would be nice just to be able to show the information inside the page, e.g. <counter></counter>. If the user wants it at the bottom just paste that last. Simple. - Aaron

tips/viewcounter.txt · Last modified: 2017-08-04 19:13 by 82.132.241.152