How to create custom message on plugin update

I saw this message today when accessing my plugin page:
custom plugin update message

So, how do I create this if I want to update my own plugins that’s hosted on wordpress?

Related posts

Leave a Reply

2 comments

  1. This message is created by W3_Total_Cache->in_plugin_update_message() hooked to "in_plugin_update_message-$file"in wp_plugin_update_row().

    It does some nifties to parse readme and display info from changelog, but overall you can just echo some stuff as with any other hook.

  2. Hook building

    To make the action hook name clear:

    global $pagenow;
    if ( 'plugins.php' === $pagenow )
    {
        // Better update message
        $file   = basename( __FILE__ );
        $folder = basename( dirname( __FILE__ ) );
        $hook = "in_plugin_update_message-{$folder}/{$file}";
        add_action( $hook, 'your_update_message_cb', 20, 2 );
    }
    

    Hooked callback function

    The function itself has two $variables attached: $plugins_data & $r, which can get accessed by your plugin.

    /**
     * Displays an update message for plugin list screens.
     * Shows only the version updates from the current until the newest version
     * 
     * @param (array) $plugin_data
     * @param (object) $r
     * @return (string) $output
     */
    function your_update_message_cb( $plugin_data, $r )
    {
        // readme contents
        $data       = file_get_contents( 'http://plugins.trac.wordpress.org/browser/YOUR_PLUGIN_FOLDER_NAME_IN_THE_OFFICIAL_REPO/trunk/readme.txt?format=txt' );
    
        // assuming you've got a Changelog section
        // @example == Changelog ==
        $changelog  = stristr( $data, '== Changelog ==' );
    
        // assuming you've got a Screenshots section
        // @example == Screenshots ==
        $changelog  = stristr( $changelog, '== Screenshots ==', true );
    
        // only return for the current & later versions
        $curr_ver   = get_plugin_data('Version');
    
        // assuming you use "= v" to prepend your version numbers
        // @example = v0.2.1 =
        $changelog  = stristr( $changelog, "= v{$curr_ver}" );
    
        // uncomment the next line to var_export $var contents for dev:
        # echo '<pre>'.var_export( $plugin_data, false ).'<br />'.var_export( $r, false ).'</pre>';
    
        // echo stuff....
        $output = 'whatever you want to do';
        return print $output;
    }
    

    Footnote:

    This approach can be found in the Internal link checker plugin.

    Addition:

    plugin_basename(__FILE__) can be used instead of those two lines above. Also checking if the current page is the plugin page is not really necessary as the function will only be called by that page anyway. The (very minor) benefit still is that you don’t have another callback attached. As this answer is quite old, you would, while this approach still works without a problem, now check against the object returned by get_current_screen().