プラグインが以前にアクティブになったときはいつでも、私のプラグインからの関数が呼び出され、(少なくとも)プラグインの名前、またはそれを識別できるもの(ファイル名は?)が返されることを望みます。
私はアクションフック "deactivated_plugin"を使うことができると思いましたが、私は非活性化されたプラグインからどのような情報が実際に利用可能か疑問に思いますか?
アクション'deactivate_plugin'
にフックして、プラグインのベース名を取得し、(2番目のパラメータとして)ネットワーク全体で無効化された場合はそれを使用できます。
wp-admin/includes/plugin.php function deactivate_plugins()
を参照してください。
プラグインを削除するためのフックはないようです。
サンプルロギングコード:
add_action( 'activated_plugin', 't5_plugin_logger', 10, 2 );
add_action( 'deactivated_plugin', 't5_plugin_logger', 10, 2 );
/**
* Log plugin activations and deactivations.
*
* @param string $plugin
* @param bool $network_wide
* @return void
*/
function t5_plugin_logger( $plugin, $network_wide )
{
$log_size = 20;
$log = get_option( 't5_plugin_log', array () );
// Remove the oldest entry.
sizeof( $log ) > $log_size and array_shift( $log );
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
$date_format = get_option( 'date_format' ) . ' · ' . get_option( 'time_format' );
$log[] = array (
'user' => esc_html( wp_get_current_user()->display_name ),
'plugin' => $plugin_data['Name'],
'network' => $network_wide ? '✔' : '',
'time' => date( $date_format, time() ),
'action' => 'deactivated_plugin' === current_filter() ? 'deactivated' : 'activated'
);
update_option( 't5_plugin_log', $log );
}
これで、すべてのプラグインの有効化または無効化がログオプションフィールドに追加されます。このデータはどこでも使用できます。私は小さなダッシュボードウィジェットを作りました:
GitHubからプラグインとしてダウンロードする: T5プラグインログ