関数 add_filter()
2014/12/05
定義ファイル :/blog/wp-includes/plugin.php
add_filter()
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 | /** * Hook a function or method to a specific filter action. * * WordPress offers filter hooks to allow plugins to modify * various types of internal data at runtime. * * A plugin can modify data by binding a callback to a filter hook. When the filter * is later applied, each bound callback is run in order of priority, and given * the opportunity to modify a value by returning a new value. * * The following example shows how a callback function is bound to a filter hook. * Note that $example is passed to the callback, (maybe) modified, then returned: * * <code> * function example_callback( $example ) { * // Maybe modify $example in some way * return $example; * } * add_filter( 'example_filter', 'example_callback' ); * * * Since WordPress 1.5.1, bound callbacks can take as many arguments as are * passed as parameters in the corresponding apply_filters() call. The $accepted_args * parameter allows for calling functions only when the number of args match. * * <strong>Note:</strong> the function will return true whether or not the callback * is valid. It is up to you to take care. This is done for optimization purposes, * so everything is as quick as possible. * * @since 0.71 * * @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them. * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, * it doesn't need to run through that process. * * @param string $tag The name of the filter to hook the $function_to_add callback to. * @param callback $function_to_add The callback to be run when the filter is applied. * @param int $priority Optional. Used to specify the order in which the functions * associated with a particular action are executed. Default 10. * Lower numbers correspond with earlier execution, * and functions with the same priority are executed * in the order in which they were added to the action. * @param int $accepted_args Optional. The number of arguments the function accepts. Default 1. * @return boolean true */ function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) { global $wp_filter, $merged_filters; $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); unset( $merged_filters[ $tag ] ); return true; } |
bool add_filter( string $tag, mixed $function_to_add [ , int $priority = 10 [ , int $accepted_args = 1 ] ] )
add_filter(‘追加される元関数’, ‘追加する関数’, ‘優先度’, ‘引数の数’ );
説明
WordPressシステムの関数にフィルター関数を追加する。
パラメータ
$tag WordPressシステムの関数名。
$function_to_add フィルター関数名。メソッドを指定する場合は配列を使用する(注意を参照)。
$priority プライオリティ値(省略時は10)。
$accepted_args フィルター関数のパラメータ数(省略時は1)。
返り値
trueを返す。
注意
クラスのメソッドを指定する場合は、オブジェクトのアドレスとメソッド名を要素にした配列を使用する。クラス内ならば、次のように記述する。
add_filter(‘関数名’, array( $this, ‘メソッド名’ ));
または
add_filter(‘関数名’, array( &$this, ‘メソッド名’ ));
使用例
ログインページのメッセージを変更するフィルター関数を追加する。
1 2 3 4 5 6 | <?php add_filter( 'login_message', 'mylogin_message' ); function mylogin_message( $message ) { return '○○へようこそ'; } ?> |
ログインページのメッセージを変更するフィルター関数を追加する(クラスの場合)。
1 2 3 4 5 6 7 8 9 10 | <?php class myclass { function myclass( ) { add_filter( 'login_message', array( &$this, 'mylogin_message' ) ); } function mylogin_message( $message ) { return '○○へようこそ'; } } ?> |