WP Statistics Shortcode

Below, you’ll find a detailed guide on how to add the WP Statistics Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP Statistics Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the WP Statistics Plugin and the shortcodes it provides:

Plugin Icon
WP Statistics

"WP Statistics is a comprehensive plugin for your WordPress site that provides detailed and accurate site stats. Get insights on visitors, page views, and more with ease."

★★★★✩ (631) Active Installs: 600000+ Tested with: 6.3.2 PHP Version: 5.6
Included Shortcodes:
  • [wpstatistics]

WP Statistics [wpstatistics] Shortcode

The WP-Statistics shortcode provides a flexible way to display various site statistics. It accepts attributes like ‘stat’, ‘time’, ‘provider’, ‘format’, and ‘id’ to customize the output. Depending on the ‘stat’ value, it can show users online, visits, visitors, page visits, searches, referrers, postcount, pagecount, commentcount, spamcount, usercount, postaverage, commentaverage, useraverage, and last post date. The ‘format’ attribute allows the output to be formatted in ‘i18n’, ‘english’, or ‘abbreviated’ styles. The shortcode returns the result based on the provided attributes.

Shortcode: [wpstatistics]

Parameters

Here is a list of all possible wpstatistics shortcode parameters and attributes:

  • stat – Determines the type of statistics to display
  • time – Specifies the time frame for the statistics
  • provider – Defines the search engine provider for the statistics
  • format – Sets the format of the output numbers
  • id – Refers to the specific page or post ID for page visit statistics

Examples and Usage

Basic example – Displaying the number of users online on your website

[wpstatistics stat="usersonline"]

Advanced examples

Displaying the number of visits to your website in the last week

[wpstatistics stat="visits" time="week"]

Displaying the number of visitors to your website from a specific provider (e.g., Google) in the last month, with the number formatted in international format

[wpstatistics stat="searches" provider="google" time="month" format="i18n"]

Displaying the number of visits to a specific page (e.g., with ID 123) in the last day

[wpstatistics stat="pagevisits" id="123" time="day"]

Displaying the number of spam comments on your website, with the number formatted in English format

[wpstatistics stat="spamcount" format="english"]

Displaying the average number of posts per user on your website

[wpstatistics stat="postaverage"]

Displaying the date of the last post on your website

[wpstatistics stat="lpd"]

PHP Function Code

In case you have difficulties debugging what causing issues with [wpstatistics] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('wpstatistics', array($this, 'shortcodes'));

Shortcode PHP function:

function shortcodes($atts)
    {

        if (!is_array($atts)) {
            return;
        }
        if (!array_key_exists('stat', $atts)) {
            return;
        }

        if (!array_key_exists('time', $atts)) {
            $atts['time'] = null;
        }
        if (!array_key_exists('provider', $atts)) {
            $atts['provider'] = 'all';
        }
        if (!array_key_exists('format', $atts)) {
            $atts['format'] = null;
        }
        if (!array_key_exists('id', $atts)) {
            $atts['id'] = -1;
        }

        $formatnumber = array_key_exists('format', $atts);

        switch ($atts['stat']) {
            case 'usersonline':
                $result = wp_statistics_useronline();
                break;

            case 'visits':
                $result = wp_statistics_visit($atts['time']);
                break;

            case 'visitors':
                $result = wp_statistics_visitor($atts['time'], null, true);
                break;

            case 'pagevisits':
                $result = wp_statistics_pages($atts['time'], null, $atts['id']);
                break;

            case 'searches':
                $result = wp_statistics_searchengine($atts['provider'], $atts['time']);
                break;

            case 'referrer':
                $result = wp_statistics_referrer($atts['time']);
                break;

            case 'postcount':
                $result = Helper::getCountPosts();
                break;

            case 'pagecount':
                $result = Helper::getCountPages();
                break;

            case 'commentcount':
                $result = Helper::getCountComment();
                break;

            case 'spamcount':
                $result = Helper::getCountSpam();
                break;

            case 'usercount':
                $result = Helper::getCountUsers();
                break;

            case 'postaverage':
                $result = Helper::getAveragePost();
                break;

            case 'commentaverage':
                $result = Helper::getAverageComment();
                break;

            case 'useraverage':
                $result = Helper::getAverageRegisterUser();
                break;

            case 'lpd':
                $result       = Helper::getLastPostDate();
                $formatnumber = false;
                break;
        }

        if ($formatnumber) {
            switch (strtolower($atts['format'])) {
                case 'i18n':
                    $result = number_format_i18n($result);

                    break;
                case 'english':
                    $result = number_format($result);

                    break;
                // In this line a new function is added so that the abbreviation of larger numbers is displayed with the symbols 1K, 1M or 1B
                case 'abbreviated':
                    $result = $this->formatNumber($result);
                    break;
            }
        }

        return $result;
    }

Code file location:

wp-statistics/wp-statistics/includes/class-wp-statistics-shortcode.php

Conclusion

Now that you’ve learned how to embed the WP Statistics Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *