How to Sanitize user IP?

If the IP value returned from your server has a special character,
you can use:wp_statistics_sanitize_user_ipfilter in your WordPress for getting real user IP from your $_SERVER.

For example:
If your $_SERVER: 192.000.000.1, 192.000.000.2, 192.000.000.3
And your real IP is: 192.000.000.1
You can use a final filter like this:

add_filter( 'wp_statistics_sanitize_user_ip', 'sanitize_user_ip' );
function sanitize_user_ip( $user_ip ) {
    $ip_list = explode( ",", $user_ip );
    $user_ip = trim( $ip_list[0] );

    return $user_ip;
}

Or if your $_SERVER: for=192.000.000.1;proto=http;host=site.com
And your real IP is: 192.000.000.1
Use this one:

add_filter( 'wp_statistics_sanitize_user_ip', 'sanitize_user_ip' );
function sanitize_user_ip( $user_ip ) {
    $regex = '/(?<=for=).*?(?=;)/';
    preg_match( $regex, $user_ip, $ip );
    $user_ip = $ip[0];

    return $user_ip;
}

You can add each code on top of your activate theme functions.php.

How stops tracking in WP-Statistics with PHP code

Sometimes you need to enable the Anonymize IP Addresses with PHP code for GDPR, in here is an example to do that.

add_action('init', function (){
global $WP_Statistics;
$WP_Statistics->update_option( 'anonymize_ips', '1' );
});

Receive the number of visits from a page in different periods of time

You can receive the number of visits from a page or post in different periods of time. It’s done with the following PHP code:

$page_id = 3; // Your page ID

// Get page hits
$today = wp_statistics_pages('today', $page_id);
$yesterday = wp_statistics_pages('yesterday', $page_id);
$week = wp_statistics_pages('week', $page_id);
$month = wp_statistics_pages('month', $page_id);
$year = wp_statistics_pages('year', $page_id);
$total = wp_statistics_pages('total', $page_id);
$range = wp_statistics_pages('-10', $page_id); // For the past 10 days

Subscribe Newsletter

Sign up with your email address to receive news and updates.