RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Display the Number of Total WordPress Downloads

Display the Number of Total WordPress Downloads

This article will show you how to retrieve the total WordPress download count and display it in a WP post or page with shortcode. Of all the excellent resources and APIs that WordPress makes available to their community, I couldn't find one that returned a total count. Some time back we had a need for it, so we wrote a quick function that would scrape the WordPress website and cache the result. While a regular expression is never a good idea for parsing HTML content - it works (until it doesn't, of course).

The Result

The result will display the download count for the current version as extracted from this page on the WordPress website. The shortcode of [wpdl] will return the following: . Made to look a little more interesting:

If you're in the business of supporting WordPress in any way, it might come in handy.

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 Display the Number of Total WordPress Downloads
4
 http://www.beliefmedia.com/wordpress-download-count
5
*/
6
 
7
function beliefmedia_wp_download_count($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'cache_temp' => 600,
11
    'cache' => 7200
12
  ), $atts);
13
 
14
 $transient = 'wpdl_' . md5(serialize($atts));
15
 $count = get_transient($transient);
16
 
17
 if ($count !== false) {
18
 return $count;
19
 
20
  } else {
21
 
22
     $text = @file_get_contents('http://wordpress.org/download/counter/');
23
 
24
     if ($text !== false) {
25
 
26
       $pattern = &quot;/<span id=\&quot;numnumnum\&quot;>(.*?)<\/span>/s&quot;;
27
       preg_match($pattern, $text, $m);
28
       $count = $m[1];
29
 
30
       /* Set transient */
31
       set_transient($transient, $count, $atts['cache']);
32
 
33
       } else {
34
 
35
       $count = 'Unavailable';
36
 
37
       /* Set temp transient */
38
       set_transient($transient, $count, $atts['cache_temp']);
39
 
40
     }
41
 
42
  }
43
 
44
 return $count;
45
}
46
add_shortcode('wpdl','beliefmedia_wp_download_count');

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

PHP Function

Used outside of WordPress, the following may be used. Usage requires Simple Cache.

1
<?php 
2
/*
3
 Display the Number of Total WordPress Downloads
4
 http://www.beliefmedia.com/wordpress-download-count
5
*/
6
 
7
8
 
9
  $atts = array(
10
    'cache_temp' => 600,
11
    'cache' => 7200
12
  );
13
 
14
 $transient = 'wpdl_count_' . md5(serialize($atts));
15
 $count = beliefmedia_get_transient($transient, $atts['cache']);
16
 
17
 if ($count !== false) {
18
 return $count;
19
 
20
  } else {
21
 
22
     $text = @file_get_contents('http://wordpress.org/download/counter/');
23
 
24
     if ($text !== false) {
25
 
26
       $pattern = &quot;/<span id=\&quot;numnumnum\&quot;>(.*?)<\/span>/s&quot;;
27
       preg_match($pattern, $text, $m);
28
       $count = $m[1];
29
 
30
       /* Set transient */
31
       beliefmedia_set_transient($transient, $count);
32
 
33
       } else {
34
 
35
       $count = beliefmedia_get_transient_data($transient);
36
 
37
     }
38
 
39
  }
40
 
41
 return $count;
42
}
43
 
44
/* Usage */
45

Download


Title: Display the Number of Total WordPress Downloads (WP Plugin)
Description: Displays a total count of WordPress downloads. Sourced from the WP website.
  Download • Version 0.2, 1.5K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (649.0B)    PHP Code & Snippets, (657.0B)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment