Hi drac022,
Thank you for writing, I hope you’re doing well.
We just released a new version of the plugin (1.0.8) that allows to customize the way clicks are tracked by adding some custom code. For example, you could add something like this to a custom plugin or your theme’s functions.php file:
add_filter( 'nice_likes_user_ip', 'custom_likes_user_ip' );
/**
* Get the visitor's IP address
*/
function custom_likes_user_ip( $ip ) {
$user = wp_get_current_user();
if ( $user->exists() ) {
$ip .= ':' . $user->user_nicename;
}
return $ip;
}
That code adds “:” followed by the username of the logged-in user to the current IP. So if I was logged in to your site, my IP would be registered as “127.0.0.1:andrezrv”. Alternatively, you could just rewrite the whole $ip variable with any unique handler that you can come up with.
So, basically, you just need to update the plugin the 1.0.8 version, and add some custom code like the example provided above.
Please let me know if this helps.
Best,
Andrés.
Hi! Thanks a lot!
Are there any way to track via cookie? Because, if the site don’t have user logins, the problem with local net works still there.
May be I can add other variable before the “:”?
Well, something like that could be possible if you set a custom cookie and use it to track the activity of users that haven’t logged in. Here’s an example:
add_action( 'init', 'custom_likes_set_cookie' );
/**
* Create a custom cookie to help track user activity.
*/
function custom_likes_set_cookie() {
// If the cookie is already set, do nothing.
if ( isset( $_COOKIE['nice_likes'] ) ) {
return;
}
$expiration_time = time() + ( 3600 * 24 ); // Set expiration time to one day from now.
setcookie( 'nice_likes', time(), $expiration_time );
}
add_filter( 'nice_likes_user_ip', 'custom_likes_user_ip' );
/**
* Get the visitor's IP address
*/
function custom_likes_user_ip( $ip ) {
$user = wp_get_current_user();
if ( $user->exists() ) {
$ip .= ':' . $user->user_nicename;
} elseif ( isset( $_COOKIE['nice_likes'] ) ) {
$ip .= ':' . $_COOKIE[ 'nice_likes' ];
}
return $ip;
}
So basically here we set a custom cookie and add it to the IP of non-logged visitors. For logged-in users, we can keep using their internal nicename, since that information is not likely to change over time.
However, keep in mind that cookies are temporal, and they’re going to be reset at some point, no matter the expiration time you set to them (that’s how cookies are supposed to work). Without a user session going on, there’s not an easy way to keep an exact track of visitor’s activity, so the “like” button is going to be available again to visitors that already clicked on it once their cookie has been reset.
An alternative to this scenario could be to disable the liking functionality for non-logged users all along. You could do that with this piece of code:
add_filter( 'nice_likes_settings', 'custom_likes_settings' );
/**
* Modify plugin settings.
*/
function custom_likes_settings( $settings ) {
// Disable likes functionality if the user is not logged in.
if ( ! is_admin() && ! is_user_logged_in() ) {
$settings['enable'] = false;
}
return $settings;
}
Please let me know if this is helpful to you.
Best,
Andrés.
Works like a charm!
Thanks Andrés!!
That’s great 🙂
Feel free to ask in case you have any other question.
Best,
Andrés.