Shortcode: Show Blog Statistics

Add following code where you want to display it.

Usage

The wcs_count shortcode has three attributes which use the following format:
[wcs_count type=’TYPE’ format=true extra=’DATA’]
The format attribute is used to insert locale-specific thousands separators (as needed). The default value is true.
The extra attribute provides additional data for some of the count types.
The available count types are:
posts (# of posts)
posts_by_author (# of posts by one user; extra is user id)
pages (# of pages)
categories (# of catagories)
tags (# of tags)
users (# of registered users)
ms_users (# of registered multi-site users)
blogroll (# of blogroll links)
blogroll_categories (# of blogroll categories)
comments (# of approved comments)
comments_pending (# of pending comments)
comments_spam (# of spam comments)
comments_pingback (# of pingbacks)
comments_by_user (# of comments by user id; extra is user id)
comments_by_nicename (# of comments by nicename; extra is nicename)
comments_by_email (# of comments by email; extra is email address)
commenters (# of unique commenters)
comments_per_post (average comments per post; extra is decimal place precision)

[wcs_count type="posts"]
[wcs_count type=pages]
[wcs_count type=commenters]
[wcs_count type=comments_per_post]

Add following snippet to “functions.php”

Snippet

add_shortcode('wcs_count', 'wcs_count_shortcode_handler');
function wcs_count_shortcode_handler($atts)
{
    // extract parameters
    $parms = shortcode_atts(array(
        'type' => 'posts',
        'format' => 'true',
        'extra' => '1',
        ), $atts);
    $type = strtolower($parms['type']);
    $format = strtolower($parms['format']);
    $extra = $parms['extra'];
    // process t/f options
    $b_format = false;
    if (($format == 'yes') || ($format == 'y') ||
        ($format == 'true') || ($format == '1'))
    {$b_format = true;}
    // exit
    return wcs_get_count($type, $b_format, $extra);
}
function wcs_get_count($type='posts', $format='1', $extra='1')
{
    // TYPES:
    // posts, posts_by_author, pages, tags, categories
    // users, ms_users, blogroll, blogroll_categories, commenters
    // comments, comments_pending, comments_spam, comments_pingback
    // comments_by_user, comments_by_nicename, comments_by_email
    // comments_per_post
    // $extra is used with:
    // posts_by_author, comments_by_user, comments_by_nicename, comments_by_email
    // comments_per_post
    // init
    global $wpdb;
    $type = strtolower($type);
    $count = 0;
    // process
    switch($type)
    {
        case 'posts': // published
        $count = wp_count_posts('post');
        $count = $count->publish;
        // options: publish, future, draft, pending, private, trash, auto-draft, & inherit
        break;
        case 'posts_by_author': // use $extra for user/author id
        case 'posts_by_user':
        $query = "SELECT COUNT(*) FROM $wpdb->posts ";
        $where = "WHERE post_type='post' AND post_status='publish' AND post_author='$extra'";
        $count = $wpdb->get_var($query . $where);
        // alternative method is: count_user_posts()
        break;
        case 'pages': // published
        $count = wp_count_posts('page');
        $count = $count->publish;
        break;
        case 'tags':
        $count = wp_count_terms('post_tag');
        break;
        case 'categories':
        $count = wp_count_terms('category');
        break;
        case 'users':
        $count = count_users();
        $count = $count['total_users'];
        break;
        case 'ms_users': // multi-site
        $count = get_user_count();
        break;
        case 'blogroll':
        $query = "SELECT COUNT(*) FROM $wpdb->links ";
        $where = "WHERE link_visible='Y'";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'blogroll_categories':
        $count = wp_count_terms('link_category');
        break;
        case 'commenters':
        $query = "SELECT COUNT(DISTINCT comment_author) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type=''";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments':
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type=''";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_pending':
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='0' AND comment_type=''";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_spam':
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='spam' AND comment_type=''";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_pingback':
        case 'comments_pingbacks':
        case 'comments_trackback':
        case 'comments_trackbacks':
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type='pingback'";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_by_user': // use $extra for user_id
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type='' AND user_id='$extra'";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_by_author': // use $extra for author nicename (case INsensitive)
        case 'comments_by_nicename':
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type='' AND comment_author='$extra'";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_by_email': // use $extra for author email (case INsensitive)
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type='' AND comment_author_email='$extra'";
        $count = $wpdb->get_var($query . $where);
        break;
        case 'comments_per_post': // $extra is decimal place precision (0 for integer only)
        // posts
        $posts_count = wp_count_posts('post');
        $posts_count = $posts_count->publish;
        // comments
        $query = "SELECT COUNT(*) FROM $wpdb->comments ";
        $where = "WHERE comment_approved='1' AND comment_type=''";
        $comment_count = $wpdb->get_var($query . $where);
        // average
        return round($comment_count / $posts_count, $extra);
        default:
        $count = 0;
    }
    // exit
    if ($format) {$count = number_format_i18n($count);}
    return $count;
    /**********************************************************************
     Copyright © 2011 Gizmo Digital Fusion (http://wpCodeSnippets.info)
     you can redistribute and/or modify this code under the terms of the
     GNU GPL v2: http://www.gnu.org/licenses/gpl-2.0.html
    **********************************************************************/
}

Courtesy: Gizmo Digital Fusion

One Response to Shortcode: Show Blog Statistics

  1. profollica says:

    If some one wants to be updated with latest technologies afterward
    he must be pay a quick visit this site and be up to date every day.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>