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 a WordPress Plugin Download Box with Links and Download Count

Display a WordPress Plugin Download Box with Links and Download Count

When we first started to submit the odd plugin to the WordPress plugin repository, we needed a means of providing an enhanced download link from our website. The answer came by way of a little download box with information sourced via the WordPress API. We wanted to include a title, basic description, download link, version details, and download count - and WP's API provides this. This article will provide you with the necessary WordPress shortcode that'll render an information download box in your WordPress page or post.

The result of our code is as follows:

Plugin Title: Author:
Description:
Download (downloaded 0 times) | Plugin Page

The shortcode I used to generate this specific information was as follows: [bmplugin name="simple-audio-player"].

There's a large amount of data returned via the WP API. To avoid hogging realestate we've added the update time and tested version text to the Download and Plugin Page links as link titles.

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 a WordPress Plugin Download Box with Links and Download Count
4
 http://www.beliefmedia.com/wordpress-download-box
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'name' => 'simple-audio-player',
11
    'title' => false,
12
    'description' => false,
13
    'author' => true,
14
    'n' => true,
15
    'trim' => 16,
16
 
17
    /* Alter appearance */
18
    'image' => 'http://www.beliefmedia.com/wp-images/site/wp-110x90.png',
19
    'bgcolor' => '#f7f7f7',
20
    'border' => '#ebebeb 2px solid',
21
 
22
    /* Cache */
23
    'cache' => 3600 * 24 * 7,
24
    'cache_error' => 3600 * 6
25
  ), $atts);
26
 
27
  $hash = md5(serialize($atts));
28
  $transient = 'wpdb_' . $hash;
29
 
30
  $cachedposts = get_transient($transient);
31
  if ($cachedposts !== false) {
32
  return $cachedposts;
33
 
34
  } else {
35
 
36
   $plugin_name = strtolower(str_replace(' ', '-', $atts['name']));
37
   $download_link .= 'http://downloads.wordpress.org/plugin/' . $plugin_name . '.zip';
38
 
39
    $args = (object) array( 'slug' => $atts['name'] );
40
    $request = array( 'action' => 'plugin_information', 'timeout' => 30, 'request' => serialize( $args) );
41
    $url = 'http://api.wordpress.org/plugins/info/1.0/';
42
    $response = wp_remote_post($url, array('body' => $request));
43
 
44
    if ($response) {
45
 
46
       /* Basic plugin info.. */
47
       $plugin_info = unserialize( $response['body'] );
48
       $plugin_title = ($atts['title'] !== false) ? $atts['title'] : $plugin_info->name;
49
       $author_profile = $plugin_info->author_profile;
50
       $requires = $plugin_info->requires;
51
       $tested = $plugin_info->tested;
52
       $homepage = $plugin_info->homepage;
53
       $download_link = $plugin_info->download_link;
54
       $plugin_version = $plugin_info->version;
55
 
56
       /* Add author after title? */
57
       if ($atts['author'] !== false) $plugin_title .= ' <strong>Author</strong>: <a href=&quot;' . $author_profile . '&quot; title=&quot;WP Profile&quot;>' . end(explode('/', rtrim($author_profile, '/'))) . '</a>';
58
 
59
       /* Number format by default */
60
       $downloads = $plugin_info->downloaded;
61
         if ($atts['n'] !== false) $downloads = number_format($downloads);
62
 
63
       /* Plugin description. Optionally trims WP description */
64
       if ($atts['description'] !== false) {
65
         $plugin_description = $atts['description'];
66
       } else {
67
         $plugin_description = $plugin_info->sections['description'];
68
         preg_match(&quot;/<p>(.*)<\/p>/&quot;, $plugin_description, $matches);
69
         $plugin_description = strip_tags($matches[1]);
70
         if ($atts['trim'] !== '') $plugin_description =  wp_trim_words($plugin_description, $num_words = $atts['trim'], $more = '&hellip;');
71
       }
72
 
73
       /* Date added to WP repository */
74
       $added = $plugin_info->added;
75
         $added = date(&quot;jS M y&quot;, strtotime($added));
76
 
77
       /* Date plugin was last updated */
78
       $last_updated = $plugin_info->last_updated;
79
         $last_updated = date(&quot;jS M y&quot;, strtotime($last_updated));
80
 
81
       /* Build box */
82
       $return = '<p><div style=&quot;margin: 0 0 25px; overflow: hidden; padding: 20px 20px 20px 120px; border: ' . $atts['border'] . '; background: ' . $atts['bgcolor'] . ' url(' . $atts['image'] . ') no-repeat 10px 50%;&quot;><strong>Plugin Title:</strong> ' . $plugin_title . '<br><strong>Description:</strong> ' . $plugin_description . '<br><a href=&quot;' . $download_link . '&quot; title=&quot;Tested to: ' . $tested . '&quot;>Download</a> (downloaded ' . $downloads . ' times) | <a href=&quot;http://wordpress.org/plugins/' . $plugin_name . '/&quot; target=&quot;_blank&quot; title=&quot;Since: ' . $added . ', Updated: ' . $last_updated . '&quot; rel=&quot;noopener noreferrer&quot;>Plugin Page</a></div></p>';
83
       set_transient($transient, $return, $atts['cache']);
84
 
85
    } else {
86
 
87
     $return = '<p><div style=&quot;margin: 0 0 25px; overflow: hidden; padding: 20px 20px 20px 120px; border: ' . $atts['border'] . '; background: ' . $atts['bgcolor'] . ' url(' . $atts['image'] . ') no-repeat 10px 50%;&quot;>Error retrieving <a href=&quot;https://wordpress.org/plugins/' . $plugin_name . '/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;>plugin data</a>. Try again soon.</div></p>';
88
     set_transient($transient, $return, $atts['cache_error']);
89
 
90
    }
91
 
92
  return $return;
93
 }
94
}
95
add_shortcode('bmplugin','beliefmedia_wordpress_download_box');

The image I've used can be downloaded from here . Alter it in your shortcode along with the background color and border style.

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.

Shortcode Attributes

name

The name of your plugin is the only attribute that is required. While the plugin name is generally accepted, the plugin slug is preferred to avoid errors.

title

The title is optional. By default, the plugin title as used in the WordPress repository will be used. To use your own use title="my title in here".

description

The description is optional. By default, the plugin title as used in the WordPress repository will be used. To use your own use description="my description in here".

author

The author name and link will be rendered alongside your plugin title. To remove, use author="0" or alter your shortcode function.

n

By default we'll return your download count using PHP's number_format() function. If you choose not return the count in this manner, use n="0" or alter the shortcode function.

trim

We trim the description to 16 words. To change, alter the shortcode function, or use in your shortcode as trim="25". It's used so long descriptions don't run longer than is aesthetically pleasing.

image

Your own image should be used. Hard-code it into the shortcode function.

bgcolor

Alter the bgcolor with bgcolor="#ffffff" or code a permanent change into the shortcode function.

border

Alter the border with border="#000000" or code a permanent change into the shortcode function.

cache & cache_error

The cache time refers to the time the result is cached. The cache_error time refers to the time you'll cache an error message before trying again.

PHP Function

The following PHP function may be used outside of WordPress. The primary differences are detailed below. Usage requires Simple Cache With PHP.

1
<?php 
2
/*
3
 Display a WordPress Plugin Download Box with Links and Download Count
4
 http://www.beliefmedia.com/wordpress-download-box
5
*/
6
 
7
function beliefmedia_wordpress_download_box($args = '') {
8
 
9
  $atts = array(
10
    'name' => 'simple-audio-player',
11
    'title' => false,
12
    'description' => false,
13
    'author' => true,
14
    'n' => true,
15
    'trim' => 140,
16
 
17
    /* Alter appearance */
18
    'image' => 'http://www.beliefmedia.com/wp-images/site/wp-110x90.png',
19
    'bgcolor' => '#f7f7f7',
20
    'border' => '#ebebeb 2px solid',
21
 
22
    /* Cache */
23
    'cache' => 3600 * 24 * 7,
24
    'cache_error' => 3600 * 6
25
  );
26
 
27
  /* Merge $args with $atts */
28
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
29
 
30
  $hash = md5(serialize($atts));
31
  $transient = 'wp_plugin_box_' . $hash;
32
 
33
  $cachedposts = beliefmedia_get_transient($transient, $atts['cache']);
34
  if ($cachedposts !== false) {
35
  return $cachedposts;
36
 
37
  } else {
38
 
39
   $plugin_name = strtolower(str_replace(' ', '-', $atts['name']));
40
   $download_link .= 'http://downloads.wordpress.org/plugin/' . $plugin_name . '.zip';
41
 
42
    /* Use file_get_contents access plugin JSON rather than wp remote post() */
43
    $url = 'http://api.wordpress.org/plugins/info/1.0/' . $plugin_name . '.json';
44
    $response = @file_get_contents($url);
45
 
46
    if ($response) {
47
 
48
       /* Basic plugin info.. */
49
       $response = json_decode($response, true);
50
       $plugin_title = ($atts['title'] !== false) ? $atts['title'] : $response['name'];
51
       $author_profile = $response['author_profile'];
52
       $requires = $response['requires'];
53
       $tested = $response['tested'];
54
       $homepage = $response['homepage'];
55
       $download_link = $response['download_link'];
56
       $plugin_version = $response['version'];
57
 
58
       /* Add author after title? */
59
       if ($atts['author'] !== false) {
60
         $author = explode(&quot;/&quot;, rtrim($author_profile, '/'));
61
         // $author = $author);
62
         $author = end($author);
63
         $plugin_title .= ' <strong>Author</strong>: <a href=&quot;' . $author_profile . '&quot; title=&quot;WP Profile&quot;>' . $author . '</a>';
64
       }
65
 
66
       /* Number format by default */
67
       $downloads = $response['downloaded'];
68
         if ($atts['n'] !== false) $downloads = number_format($downloads);
69
 
70
       /* Plugin description. Optionally trims WP description */
71
       if ($atts['description'] !== false) {
72
 
73
         $plugin_description = $atts['description'];
74
 
75
       } else {
76
 
77
         $plugin_description = $response['sections']['description'];
78
         preg_match(&quot;/<p>(.*)<\/p>/&quot;, $plugin_description, $matches);
79
         $plugin_description = strip_tags($matches[1]);
80
         if ($atts['trim'] !== '') $plugin_description = trim(mb_strimwidth($plugin_description, 0, $atts['trim'], ' ...'));
81
 
82
       }
83
 
84
       /* Date added to WP repository */
85
       $added = $response['added'];
86
         $added = date(&quot;jS M y&quot;, strtotime($added));
87
 
88
       /* Date plugin was last updated */
89
       $last_updated = $response['last_updated'];
90
         $last_updated = date(&quot;jS M y&quot;, strtotime($last_updated));
91
 
92
       /* Build box */
93
       $return = '<p><div style=&quot;margin: 0 0 25px; overflow: hidden; padding: 20px 20px 20px 120px; border: ' . $atts['border'] . '; background: ' . $atts['bgcolor'] . ' url(' . $atts['image'] . ') no-repeat 10px 50%;&quot;><strong>Plugin Title:</strong> ' . $plugin_title . '<br><strong>Description:</strong> ' . $plugin_description . '<br><a href=&quot;' . $download_link . '&quot; title=&quot;Tested to: ' . $tested . '&quot;>Download</a> (downloaded ' . $downloads . ' times) | <a href=&quot;http://wordpress.org/plugins/' . $plugin_name . '/&quot; target=&quot;_blank&quot; title=&quot;Since: ' . $added . ', Updated: ' . $last_updated . '&quot; rel=&quot;noopener noreferrer&quot;>Plugin Page</a></div></p>';
94
       beliefmedia_set_transient($transient, $return);
95
 
96
    } else {
97
 
98
     $return = '<p><div style=&quot;margin: 0 0 25px; overflow: hidden; padding: 20px 20px 20px 120px; border: ' . $atts['border'] . '; background: ' . $atts['bgcolor'] . ' url(' . $atts['image'] . ') no-repeat 10px 50%;&quot;>Error retrieving <a href=&quot;https://wordpress.org/plugins/' . $plugin_name . '/&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;>plugin data</a>. Try again soon.</div></p>';
99
     beliefmedia_set_transient($transient, $return);
100
 
101
    }
102
 
103
  return $return;
104
 }
105
}
106
 
107
/* Usage */
108

The primary difference between this and the WP version is that we truncate the description text to a character length - not a word count. If our means of truncation doesn't agree with you, select an alternative function here.

Considerations

  • You don't need to have anything to do with any of the plugins to be able to retrieve and display the data, making it an effective way of displaying information on review sites and blogs.
  • For inactive plugins, consider using a longer cache period to avoid repeated requests to the WordPress API.
  • A collection of the APIs used on the WordPress website are available here .

Download


Title: Display a WordPress Plugin Download Box
Description: Display a WordPress Plugin Download Box with Links and Download Count.
  Download • Version 0.2, 12.2K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (1.6K)    PHP Code & Snippets, (1.7K)    

■ ■ ■

 
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