Say beautiful and wonderful people! I’m looking for a view count plugin for my site’s posts. I would like, if possible, to point me to one made especially for ClassicPress. Thank you very much!
I found a snippet in an utility plugin. It was coded for a client but never used, so it never became a plugin.
Please consider that it was poorly tested.
If you find it useful, here it is:
class MyCounter{
public $counter = [];
public function __construct() {
add_filter('the_content', [$this, 'increment']);
add_shortcode('xsxcounter', [$this, 'shortcode']);
}
public function increment($content) {
if(!is_singular('post') || !in_the_loop()) {
return $content;
}
global $post;
if ($this->counter === []) {
$counter = get_option('xsx_counter', []);
}
$counter[$post->ID] = isset($counter[$post->ID]) ? $counter[$post->ID]+1 : 1;
update_option('xsx_counter', $counter);
return $content;
}
public function shortcode(){
global $post;
if ($this->counter === []) {
$counter = get_option('xsx_counter', []);
}
return $counter[$post->ID];
}
}
new MyCounter;
It keep track of single post view and you can display the number of visits using [xsxcounter]
shortcode.
1 Like
People typically don’t like being counted. That said, the code snippet is lacking an easy way to show the count without changing each post/page to put a shortcode.
1 Like
You hit the spot
My client wanted that feature but finally I’ve convinced him that people don’t like to be counted, so I stopped working on that code!
2 Likes
Strange that people are bothered by the view counter. However, I would like a plugin that actually works. I don’t have snippets skills