WordPressで有効になっているプラグインと読み込まれる順番を調べる方法

WordPressで有効になっているプラグインと読み込まれる順番を調べる方法

WordPressで有効になっているプラグインと読み込まれる順番を調べる方法

WordPressで有効になっているプラグインと、それらが読み込まれる順番を調べる方法について、NxWorldさんで解説されていましたので、ご紹介したいと思います。

スポンサードリンク

有効になっているプラグインと読み込まれる順番を調べる方法

テーマの中に以下を記述することで、有効になっているプラグインが読み込まれている順番で出力されます。

<?php var_dump( get_option( 'active_plugins' ) ); ?>

上記を記述することで、以下のように読み込み順でプラグイン名が出力されます。

array(7) { [0]=> string(36) "contact-form-7/wp-contact-form-7.php" [1]=> string(45) "enhanced-search-form/enhanced-search-form.php" [3]=> string(27) "theme-check/theme-check.php" [4]=> string(25) "usc-e-shop/usc-e-shop.php" [5]=> string(41) "wordpress-importer/wordpress-importer.php" [6]=> string(47) "wp-admin-ui-customize/wp-admin-ui-customize.php" [7]=> string(41) "wp-multibyte-patch/wp-multibyte-patch.php" }

preで囲うと見やすくなるとのことで、試してみました。

<pre><?php var_dump( get_option( 'active_plugins' ) ); ?></pre>

このようにpreで包括することで、ちゃんと改行されて出力されます。


array(7) {
  [0]=>
  string(36) "contact-form-7/wp-contact-form-7.php"
  [1]=>
  string(45) "enhanced-search-form/enhanced-search-form.php"
  [3]=>
  string(27) "theme-check/theme-check.php"
  [4]=>
  string(25) "usc-e-shop/usc-e-shop.php"
  [5]=>
  string(41) "wordpress-importer/wordpress-importer.php"
  [6]=>
  string(47) "wp-admin-ui-customize/wp-admin-ui-customize.php"
  [7]=>
  string(41) "wp-multibyte-patch/wp-multibyte-patch.php"
}

圧倒的に見やすさが違いますね。

独自プラグインの読み込み順を最初か最後にする方法

プラグインの読み込み順を変更する方法についても解説されていました。

最初に読み込むようにする

プラグインが最初に読み込まれるようにするためには、プラグインのファイル内に以下を追記します。


add_filter( 'pre_update_option_active_plugins', 'high_priority_active_plugins' );
function high_priority_active_plugins( $active_plugins, $old_value ) {
  $this_plugin = str_replace( wp_normalize_path( WP_PLUGIN_DIR ).'/', '', wp_normalize_path(  __FILE__ ) );
  foreach ( $active_plugins as $no=>$path ) {
    if ( $path == $this_plugin ) {
      unset( $active_plugins[$no] );
      array_unshift( $active_plugins, $this_plugin );
      break;
    }
  }
  return $active_plugins;
}

最後に読み込むようにする

プラグインが最後に読み込まれるようにするためには、プラグインのファイル内に以下を追記します。


add_filter( 'pre_update_option_active_plugins', 'low_priority_active_plugins' );
function low_priority_active_plugins( $active_plugins, $old_value ) {
  $this_plugin = str_replace( wp_normalize_path( WP_PLUGIN_DIR ).'/', '', wp_normalize_path(  __FILE__ ) );
  foreach ( $active_plugins as $no=>$path ) {
    if ( $path == $this_plugin ) {
      unset( $active_plugins[$no] );
      $active_plugins[] = $this_plugin;
      break;
    }
  }
  return $active_plugins;
}

あとがき

サイトのカスタマイズ中、不具合が起こった場合に使えそうです。読み込まれる順番が原因で問題が起こることって意外とありますからね。

この記事が気に入ったら
いいね!してね♪

Twitter で
スポンサードリンク

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です