How to show a notice to plugin update

I would like to show an admin notice (https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices) when my plugin has a update.

Like this:

Read More

enter image description here

How to do this?

Related posts

Leave a Reply

1 comment

  1. Assuming that your plugin is in the WordPress repository, then you can do this by hooking into the WordPress update functionality. Note that get_site_transient() is always used by falls back to the get_option() when it is not a multisite install..

    // Your plugin file
    $plugin = 'plugin-dir/plugin-file.php';
    
    // Check for Plugin updates, if you want to (not recommended for all pages)
    // wp_update_plugins();
    
    // Results of the update check
    $update_plugins = get_site_transient( 'update_plugins' );
    if ( isset( $update_plugins->response[ $plugin ] ) ) {
        // Your plugin needs an update
    }
    

    In the result of update_plugins the $update_plugins->response is an array() that will be something like this:

    'response' => 
      array (
        'plugin-dir/plugin-file.php' => 
        stdClass::__set_state(array(
           'id' => '12345',
           'slug' => 'plugin-file',
           'plugin' => 'plugin-dir/plugin-file.php',
           'new_version' => '1.2.3',
           'url' => 'https://wordpress.org/plugins/plugin-dir/',
           'package' => 'https://downloads.wordpress.org/plugin/plugin-dir.1.2.3.zip',
        )),
    ),