Displaying the number of updates available in the Admin area

The question:

I am having an issue finding out how to display the number of plugins/updates available to call somewhere else other than the admin header. I found the function wp_get_update_data should be what I need:

How is the ” wp_get_update_data ” function used?

However, I wasn’t sure how to get this to display as an actual count of the total plugins and updates available or any working example on the internet of how to use it.

Any suggestions would be much appreciated.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Here’s an example of the data returned from the wp_get_update_data() function:

Array
(
    [counts] => Array
        (
            [plugins] => 3
            [themes] => 2
            [wordpress] => 0
            [translations] => 0
            [total] => 5
        )

    [title] => 3 Plugin Updates, 2 Theme Updates
)

So the number of available plugin updates should be available with:

// Number of available plugin updates:
$update_data = wp_get_update_data();
echo $update_data['counts']['plugins'];

Update:

To display the following plugin info in the admin area:

There are available updates for 3 plugins out of 22

we can additionally use the get_plugins() function:

if ( ! function_exists( 'get_plugins' ) )
{
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

$data = array( 
    'updates'   =>  $update_data['counts']['plugins'],
    'total'     =>  count( get_plugins() ),
);

printf( 
    "There are available updates for <strong>%d</strong> plugins  
     out of <strong>%d</strong>",
    $data['updates'],
    $data['total']
);

We can add more info, in a similar way, with get_mu_plugins() and get_dropins().

Method 2

wp_get_update_data() returns an array in this format

  • counts
    • plugins
    • themes
    • wordpress
    • translations
    • total
  • title

So, if you want total count, you need to use it like this

$updates = wp_get_update_data();
echo $updates['counts']['total'];


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment