11

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?

2 Answers 2

9

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.

1
  • Ah, that hook is what i'm looking for. Thx
    – ariefbayu
    Sep 20, 2010 at 13:12
14

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().

5
  • Is there a way to test this prior to deploying the plugin to the WP svn repo?
    – MikeiLL
    Jun 3, 2020 at 15:40
  • 1
    @MikeiLL Yes. Download the contents of the file from http://plugins.trac.wordpress.org/browser/YOUR_PLUGIN_FOLDER_NAME_IN_THE_OFFICIAL_REPO/trunk/readme.txt?format=txt' as seen in the answer. Put it in a local file, change what you need. Then change the link to target your local file. The function can read local file contents without a problem. See the PHP docs for that.
    – kaiser
    Jun 4, 2020 at 7:56
  • When you say "change the link", do you mean the target for file_get_contents?
    – MikeiLL
    Jun 5, 2020 at 3:25
  • 1
    @MikeiLL yes. You could simply try it out with one text file and simple one line script that echos the other files contents to the command line.
    – kaiser
    Jun 7, 2020 at 1:51
  • 2
    Any reason why are you returning the print return value? (which always returns 1) also why don!t use the more general echo? And if you would happen to be updating the answer, please also update the callback to be an anonymous function instead of the hardcoded name? :) Thanks :) (I know it was 2011 back then, but it's 2020 :) ) - otherwise the solution still works :)
    – jave.web
    Jun 13, 2020 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.