Answers:
要明确动作钩子名称:
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 );
}
该函数本身有两个$variables
附加符:$plugins_data
&$r
,可以通过您的插件访问。
/**
* 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;
}
脚注:
可以在内部链接检查器插件中找到此方法。
加成:
plugin_basename(__FILE__)
可以代替上面的这两行使用。另外,实际上也不必检查当前页面是否为插件页面,因为无论如何该函数只会被该页面调用。(非常小的)好处仍然是您没有附加其他回调。由于此答案很旧,您可以在此方法仍然可以正常使用的同时,现在检查所返回的对象get_current_screen()
。