web-dev-qa-db-ja.com

プラグインのアップデート時にカスタムメッセージを作成する方法

私のプラグインページにアクセスしたとき、私は今日このメッセージを見ました: custom plugin update message

それで、私がワードプレスでホストされている私自身のプラグインを更新したい場合、どうやってこれを作成するのですか?

10
ariefbayu

このメッセージは、W3_Total_Cache->in_plugin_update_message()"in_plugin_update_message-$file"にフックされたwp_plugin_update_row()によって作成されます。

それはreadmeを解析してchangelogから情報を表示するためにいくつかの気の利いたことを行いますが、全体的にあなたは他のフックと同じようにいくつかのものをエコーすることができます。

9
Rarst

フックビル

アクションフック名を明確にするには:

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 );
}

フックされたコールバック関数

関数自体には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にあります。

追加:

上記の2行の代わりにplugin_basename(__FILE__)を使用できます。また、現在のページがプラグインページであるかどうかを確認することは、実際には必要ではありません。機能はいずれにせよそのページによってのみ呼び出されるためです。 (ごくわずかな)利点は、別のコールバックが添付されていないことです。この答えはかなり古いので、このアプローチはまだ問題なく動作しますが、今度は get_current_screen() によって返されるオブジェクトと照合します。

10
kaiser