RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Display WordPress Admin (Plugin Upgrade) Notices

Display WordPress Admin (Plugin Upgrade) Notices

Because we'll be sharing a lot of code outside of the WordPress plugin ecosystem, we needed a way to communicate updates to plugins (or shortcodes) when required. While we can 'force' our clients to use a global plugin that accomplishes what we've just described (and a lot more), it's not appropriate for the average user that might use a plugin of ours and then rarely check back in. With a view to making the simple plugins we share more scalable, we've decided to use a WordPress hook function with each plugin that will render an update message (and link) via a WordPress admin notice whenever it's required. It's made possible via the funky little API we're using with our download manager.

Admin Notices

Admin notices are rendered in your WP administration dashboard as messages at the top of your page. While they're often used by plugin developers for success, failure, and other messages, they can be used for any purpose. If you have a multi-author website they're an effective way of broadcasting a message (although dashboard custom meta boxes are likely more effective).

Display WordPress Admin (Plugin Upgrade) Notices

Image: An admin notice in the WordPress Administration Dashboard.

Example: If you want to display a generic admin notice to all your authors as they visit their dashboard (as pictured above), use the following.

1
<?php 
2
/*
3
 Display WordPress Admin (Plugin Upgrade) Notices
4
 http://www.beliefmedia.com/wordpress-plugin-admin-notices
5
*/
6
 
7
function beliefmedia_admin_notice($notice = '1', $from = '', $to = '') {
8
 
9
  /* Notice type */
10
  switch ($notice) {
11
      case 1:
12
          $notice = 'notice-info';
13
          break;
14
      case 2:
15
          $notice = 'notice-warning';
16
          break;
17
      case 3:
18
          $notice = 'notice-error';
19
          break;
20
  }
21
 
22
  /* If no times .. */
23
  $time = time();
24
  if ($from == '') $from = time();
25
  if ($to == '') $to = $time + 3600;
26
 
27
  /* Valid? */
28
  $valid = ( ($from >= $time) && ($time < $to) ) ? true : false;
29
 
30
  /* Message */
31
  if ($valid) {
32
    $message = '<div class=&quot;notice ' . $notice . '&quot;><p><strong>This is an example of an admin notice. Visit the post at <a href=&quot;http://www.beliefmedia.com/wordpress-plugin-admin-notices&quot; class=&quot;thickbox&quot;>www.BeliefMedia.com</a>.</strong></p></div>';
33
    echo $message;
34
  } else {
35
    return false;
36
 }
37
}
38
add_action('admin_notices', 'beliefmedia_admin_notice');

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

To render the notice only between a certain timeframe, use the from and to arguments. Different styles can be applied by using $notice="1" (notice-info), $notice="2" (notice-warning), or $notice="3" (notice-error).

The admin notice will render on every single administration page. To display on only the front dashboard page, use the following action (uses the load-(page) hook):

1
<?php 
2
add_action( 'load-index.php',  function() { add_action( 'admin_notices', 'beliefmedia_admin_notice' ); } );

Plugin Update Notices Shortcode

The following code will be of value to those of our clients that use our new download manager. For plugins distributed company-wide (without our oversight), the admin notice is an effective means of providing update notifications. For the purpose of the example, we'll use the function packaged with an upcoming post on pastebin shortcode.

1
<?php 
2
/*
3
 Check for Pastebin Updates
4
 http://www.beliefmedia.com/pastebin-php-wordpress
5
*/
6
 
7
function beliefmedia_pastebin_check_version() {
8
 
9
  $atts = array(
10
    'downloadkey' => '68de998de136873483d255031cca800e',
11
    'version' => '0.1',
12
    'plugin_name' => 'BeliefMedia Pastebin',
13
    'plugin_url' => 'http://www.beliefmedia.com/pastebin-php-wordpress',
14
    'notice' => 'notice-info', /* notice-info, notice-warning, notice-error */
15
    'cache' => 3600 * 24 * 28
16
  );
17
 
18
  /* Get cached result */
19
  $transient = 'pbud_' . md5(serialize($atts));
20
  $cachedresult =  get_transient($transient);
21
 
22
    if ($cachedresult !== false ) {
23
     $return = (boolean) ($cachedresult['download']['version'] > $atts['version']) ? true : false;
24
    } else {
25
 
26
       /* Get version details via API */
27
       $data = @file_get_contents('http://api.beliefmedia.com/downloads/details.php?id=' . $atts['downloadkey']);
28
       if ($data !== false) $data = json_decode($data, true);
29
 
30
         if ($data['status'] == '200') {
31
 
32
           /* Check current version */
33
           $return = (boolean) ($data['download']['version'] > $atts['version']) ? true : false;
34
 
35
           /* Cache new version details */
36
           set_transient($transient, $data, $atts['cache']);
37
 
38
          } else {
39
           $return = false;
40
         }
41
 
42
    }
43
 
44
 if ($return !== false) echo '<div class=&quot;notice ' . $atts['notice'] . '&quot;><p><strong>There is an update to the ' . $atts['plugin_name'] . ' Plugin. Download the updated version <a href=&quot;' . $atts['plugin_url'] . '&quot; class=&quot;thickbox&quot;>here</a>.</strong></p></div>';
45
 else return false;
46
}
47
add_action('admin_notices', 'beliefmedia_pastebin_check_version');

With each plugin we package up, the $atts array and function name is altered. We'll cache version data for up to a month. When the version returned via our API differs from that of the plugin, we'll display the notice in the administration dashboard.

Download

The downloadable code includes only the first function (labeled as shortcode).


Title: Display WordPress Admin Notices
Description: Display WordPress Admin (Plugin Upgrade) Notices. We use this feature for upgrade notifications.
  Download • Version 0.1, 704.0B, zip, Category: WordPress Shortcodes

■ ■ ■

 
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