To add front-end post views count?
-
Hi, I have a “Post Views Counter” plugin on my website, and it shows a views counter at the beginning of each post. Here is, how it looks: https://ibb.co/xgwrHct
Your plugin counts post views as well. So, my idea is: maybe you should add this counter at front-end as well? Because the plugin, that I mentioned above, counts views very bad (probably 2-3 times less, than the actual stats).
The page I need help with: [log in to see the link]
-
Hi @giorgos93,
I see that the views count on your site is being displayed on the “meta” section of the post. Did you put it there or was it injected in there automatically by the Post Views Counter plugin?
@hcabrera I added meta-item and fa fa-eye class in functions.php file of the plugin.
Also, I added this code in WordPress’ content-single.php file, after <?php if ( $meta = shamrock_get_meta_data() ) : ?> :
<div class="entry-meta">
<?php
$namehere = pvc_post_views( $post_id = 0, $echo = true );
echo $meta, $namehere;
?>
</div>Alright, in that case you can get the views count of the current post using the wpp_get_views() function. For example:
<div class="entry-meta">
<?php
if ( function_exists('wpp_get_views') ) {
echo '<i class="fa fa-eye"></i> <span>' . wpp_get_views( get_the_ID() ) . '</span>';
}
?>
</div>Hi, @hcabrera
I’ve just tested the code, that you gave me above. It does work, but it counts every visit from the same user, on the same page (at least in incognito mode). So, If I refresh the page 5 times, then the view count will increase +5 views.
Is it possible to restrict every post with the rule “1 view from 1 unique user on 1 page in 24 hours”, or smth like that?
And also, does it count my visits as well (in admin role)? If yes, then can I exclude myself somehow?
And since I wrote here, I want to ask another question:
I read, that in newer versions .widget class was removed from your plugin. I’ve just checked: in the past I changed styles for .widget_custom_html class.
Was .widget_custom_html class removed as well, or everything will still work fine?
And I’ve just noticed a new script, when updated your plugin:
<script type="text/javascript" id="wpp-js" src="https://site.ru/wp-content/plugins/wordpress-popular-posts/assets/js/wpp.min.js" data-sampling="0" data-sampling-rate="100" data-api-url="https://site.ru/wp-json/wordpress-popular-posts" data-post-id="0" data-token="***" data-lang="0" data-debug="0"></script>
Just wanted to ask, what is it for? And what will happen, if I unload it?
Hey @giorgos93,
Is it possible to restrict every post with the rule “1 view from 1 unique user on 1 page in 24 hours”, or smth like that?
Technically yes, it is possible. However, WordPress Popular Posts doesn’t collect any user identifiable data (like IP addresses for example) for privacy and GDPR compliance reasons and so it can’t do any advanced stuff like counting “unique” pageviews like Google Analytics does for example.
You could try using any of the filter/action hooks provided by the plugin to programmatically change how it tracks views. That’s assuming that you’re at least somewhat familiar with WordPress development in general. If you’re not familiar with WordPress development though then you may need to reach out to a developer so they can implement this functionality for you. Feel free to reach out if you have any specific technical questions and I’ll try to help.
And also, does it count my visits as well (in admin role)? If yes, then can I exclude myself somehow?
Yes, by default the plugin tracks all visits – including the ones from logged-in users.
WordPress Popular Posts provides an option to track views from visitors only. You can find it via WP Dashboard > Settings > WordPress Popular Posts > Tools, under the Data section.
And since you may ask, there’s currently no option to ignore views from specific user roles (eg. Administrator). You could use the various filter/action hooks mentioned earlier to programmatically have the plugin ignore visits from specific user roles or even from specific users.
If you have any other questions please let me know.
And I’ve just noticed a new script (…)
It’s not really new, the plugin has been using that script for years at this point. That script keeps track of pageviews, loads your popular posts list, etc. You just didn’t notice it was there until now.
Was .widget_custom_html class removed as well, or everything will still work fine?
No, just the .widget class was removed.
- This reply was modified 2 months, 4 weeks ago by Hector Cabrera.
Thanks a lot for a quick and detailed answers!
Yes, I am not that experienced in WP development, so I’ll try to forget that inique views count thing ??
However, I’d still like to try your post views count option. But I want to imitate the style, like here: https://litfan.ru/poleznye-resursy-dlya-pisatelej/gde-razmestit-rasskaz-v-internete-top-10-sajtov/
Here is, what I am talking about: https://postimg.cc/dDxvyCzt
Can you please give me an advice, how to modify your code above, so it’ll contain not only an eye icon, but ПРОСМОТРОВ: text (it means ‘views’) before views number, and a calendar icon with a post publication date? Here is a current code:
<div class="entry-meta">
<?php
if ( function_exists('wpp_get_views') ) {
echo '<i class="fa fa-eye"></i> <span>' . wpp_get_views( get_the_ID() ) . '</span>';
}
?>
</div>And also, can I change somehow margins etc for these icons, text?
To add that text you could simply do something like this:
<div class="entry-meta">
<?php
if ( function_exists('wpp_get_views') ) {
echo '<i class="fa fa-eye"></i> <span>ПРОСМОТРОВ:</span> <span>' . wpp_get_views( get_the_ID() ) . '</span>';
}
?>
</div>Not sure about the calendar stuff since I don’t have access to your site’s code. You may need to reach out to your developer for assistance with that one.
You may need to reach out to your developer for assistance with that one.
Unfortunately, it is not my website’s code. The calendar with post’s publication date is put by Posts Views Counter plugin (by this code, in my case):
<div class="entry-meta">
<?php
$myviews77 = pvc_post_views( $post_id = 0, $echo = true );
echo $meta, $myviews77;Can I offer an idea for your plugin, to put a calendar icon with post’s publication date as well? (in addition to other parameters, that you offer for wpp_get_views)
Thanks for the suggestion, but displaying the publish date of a post is usually the theme’s job and not something that a plugin should be doing.
Anyways, please give this a try and see how it works:
<div class="entry-meta">
<?php
if ( function_exists('wpp_get_views') ) {
echo '<div class="meta-item"><i class="fa fa-eye"></i> <span>ПРОСМОТРОВ:</span> <span>' . wpp_get_views( get_the_ID() ) . '</span></div>';
}
?>
<div class="meta-item">
<i class="fa fa-calendar"></i> <span class="updated"><?php the_date( 'd.m.Y', get_the_ID() ); ?></span>
</div>
</div>And also: I want to change number format, by putting ‘false’ there. I read your documentation, but I didn’t understand, where exactly (and what type of code) do I need to put, to set it to ‘false’? :
<div class="entry-meta">
<?php
if ( function_exists('wpp_get_views') ) {
echo '<i class="fa fa-eye"></i> <span>ПРОСМОТРОВ:</span> <span>' . wpp_get_views( get_the_ID() ) . '</span>';
}
?>
</div>To disable number formatting do this:
wpp_get_views( get_the_ID(), 'all', false )
Thanks a lot! I did try your code, and it worked almost perfectly, with 1 exception: for some reason, it added ’85’ before the actual post publication date. So it looks like this:
- You must be logged in to reply to this topic.