Abbreviate view counts

Format Top 10 counts with k, m, bn and tn suffixes instead of displaying every digit.

What it changes

This example turns the count returned by Top 10 into an abbreviated string. It uses k from 1,000, m from 1,000,000, bn from 1,000,000,000 and tn from 1,000,000,000,000.

What to change

Edit the suffixes in $thresholds or the precision passed to round(). Counts below 1,000 are returned as strings without a suffix.

This changes the representation returned by the count filter, not the stored number of views. Check integrations that expect a numeric return value.

Requirements

Source

top-10/outputs/tptn-restyle-count.php
<?php
/**
 * Plugin Name: Top 10 - Restyle count
 * Plugin URI:  https://webberzone.com/plugins/top-10/
 * Description: Reformats the Top 10 counter to show k, m, bn and tn instead.
 * Author: Ajay D'Souza
 * Author URI: https://webberzone.com
 * Version: 1.1
 *
 * @package Top_Ten
 * @license GPL-2.0+
 */

/**
 * Modify the display of the Top 10 counter to show k, m, bn and tn instead.
 *
 * @since 1.0
 *
 * @param  int $input Raw post count.
 * @return string Formatted count string.
 */
function tptn_restyle_count( $input ) {

	$input = absint( $input );

	$thresholds = array(
		1000000000000 => 'tn',
		1000000000    => 'bn',
		1000000       => 'm',
		1000          => 'k',
	);

	foreach ( $thresholds as $value => $suffix ) {
		if ( $input >= $value ) {
			return round( $input / $value, 1 ) . $suffix;
		}
	}

	return (string) $input;
}
add_filter( 'tptn_post_count_only', 'tptn_restyle_count' );

Details

Type
Plugin
Last updated
GitHub
Source · Edit companion · History

Developer references