えー、タイトルの通りなんですが WordPress でプラグイン開発している時に非常に便利な関数として get_file_data() ってのがあります。
いま、久しぶりに Codex みたら、ほとんど説明を放棄してますね。
最近出た WordPressプラグイン開発のバイブルっていう本でちらっと触れておいたので、そこから引用します。
12$plugin_data
= get_file_data(
__FILE__
,
array
(
'version'
=>
'Version'
));
$plugin_version
=
$plugin_data
[
'version'
];
プラグインやテーマのヘッダー情報を取得するには get_file_data() 関数を使用します。
プラグインの冒頭にはコメント形式でプラグイン名やバージョン、テキストドメインがヘッダー情報として書かれています。
たとえば、プラグインのバージョン情報をこちらから読み出し、wp_enqueue_script() 関数で JavaScript のバージョン情報を変更したり、翻訳ファイルのテキストドメインやドメインパスなどを取得して load_textdomain() 関数で翻訳ファイルを読み込むときに使用できると思います。
プラグインのヘッダー情報を使って一元管理ができるため、ソース中にハードコードする必要がなくなります。
また、他のプラグインやテーマのバージョンに依存するような処理を書きたい場合にも使えるでしょう。WordPress に最初からついてくるプラグイン Hello Dolly のメタ情報を取得した結果は以下の通りです。
1234567891011$data
= get_file_data( ABSPATH.
'wp-content/plugins/hello.php'
,
array
(
'name'
=>
'Plugin Name'
,
'version'
=>
'Version'
,
'uri'
=>
'Plugin URI'
,
'description'
=>
'Description'
,
'author'
=>
'Author'
,
'author_uri'
=>
'Author URI'
,
'textdomain'
=>
'Text Domain'
,
'textdomain_path'
=>
'Domain Path'
,
));
var_dump(
$data
);
123456789101112131415161718array
(8) {
[
"name"
]=>
string(11)
"Hello Dolly"
[
"version"
]=>
string(3)
"1.6"
[
"uri"
]=>
string(41)
"http://wordpress.org/plugins/hello-dolly/"
[
"description"
]=>
string(295)
"This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page."
[
"author"
]=>
string(14)
"Matt Mullenweg"
[
"author_uri"
]=>
string(13)
"http://ma.tt/"
[
"textdomain"
]=>
string(0)
""
[
"textdomain_path"
]=>
string(0)
""
}
実は、この get_file_data() 関数なんですが、WordPress プラグインの規定のヘッダ情報だけでなく、独自にセットした情報も取ってくることができるんです。
最近、魍魎ってプラグインをアップデートしたんです。
このプラグインの中で PHP 5.4 以降でしか使えない構文を使ってるんで 5.3, 5.2 の環境で使ってる人がプラグインを有効化すると Fatal Error を吐くってことなので、PHP 5.4 以上じゃない場合は、プラグインを有効化できないようにするロジックを追加しました。
こんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?php /* Plugin Name: Spirits and Goblins Version: 0.3.2 Plugin URI: https://github.com/wokamoto/spirits-and-goblins Description: This plugin enables 2-step verification using one-time password when you log in your WordPress. Author: wokamoto Author URI: https://dogmap.jp/ Text Domain: spirits-and-goblins Domain Path: /languages/ Support PHP Version: 5.4 License: Released under the GPL license http://www.gnu.org/copyleft/gpl.html Copyright 2013-2014 wokamoto (email : wokamoto1973@gmail.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ $this_plugin_info = get_file_data( __FILE__ , array ( 'version' => 'Version' , 'text_domain' => 'Text Domain' , 'domain_path' => 'Domain Path' , 'minimum_php' => 'Support PHP Version' , )); // Require PHP ver.5.4 or higer if ( version_compare(phpversion(), $this_plugin_info [ 'minimum_php' ]) >= 0) { if ( ! class_exists ( 'SpiritsAndGoblins_Admin' ) ) require (dirname( __FILE__ ). '/includes/class-SpiritsAndGoblins_Admin.php' ); if ( ! class_exists ( 'SpiritsAndGoblins' ) ) require (dirname( __FILE__ ). '/includes/class-SpiritsAndGoblins.php' ); load_plugin_textdomain( $this_plugin_info [ 'text_domain' ], false, dirname(plugin_basename( __FILE__ )) . $this_plugin_info [ 'domain_path' ]); // Go Go Go! $spirits_and_goblins = SpiritsAndGoblins::get_instance(); $spirits_and_goblins ->init(); register_activation_hook( __FILE__ , array ( $spirits_and_goblins , 'activate' )); register_deactivation_hook( __FILE__ , array ( $spirits_and_goblins , 'deactivate' )); if (is_admin()) { $spirits_and_goblins_admin = SpiritsAndGoblins_Admin::get_instance(); $spirits_and_goblins_admin ->init(); } } else { $plugin_notice = sprintf( __( 'Oops, this plugin will soon require PHP %s or higher.' , $this_plugin_info [ 'text_domain' ]), $this_plugin_info [ 'minimum_php' ]); register_activation_hook( __FILE__ , create_function( '' , "deactivate_plugins('" .plugin_basename( __FILE__ ). "'); wp_die('{$plugin_notice}');" )); } unset( $this_plugin_info ); |
ヘッダー情報に "Support PHP Version:" ってのを追加して、それを get_file_data() で取得していることが分かると思います。