我已经看过好几次了,但我不明白的是:functions.php
定义一个函数,然后将其附加到挂钩,如下所示(简化示例):
function do_stuff($a, $b) {
// Do stuff with $a and $b
}
add_filter( \'bloginfo_url\', \'do_stuff\', 10, 2 );
基本上我想我知道那里发生了什么,但我怎么知道呢$a
和$b
是吗在“传统”PHP方式中,可以这样调用函数:
do_stuff("var a content", $var_b_content);
那很清楚什么$a
和$b
包含,但我怎么能用Wordpress知道呢?现实生活中的示例,以以下函数为例(贷记到Frank Bültge):
if ( ! function_exists( \'fb_css_cache_buster\' ) ) {
function fb_css_cache_buster( $info, $show ) {
if ( ! isset($pieces[1]) )
$pieces[1] = \'\';
if ( \'stylesheet_url\' == $show ) {
// Is there already a querystring? If so, add to the end of that.
if ( strpos($pieces[1], \'?\' ) === FALSE ) {
return $info . "?" . filemtime( WP_CONTENT_DIR . $pieces[1] );
} else {
$morsels = explode( "?", $pieces[1] );
return $info . "&" . filemtime( WP_CONTENT_DIR . $morsles[1] );
}
} else {
return $info;
}
}
add_filter( \'bloginfo_url\', \'fb_css_cache_buster\', 9999, 2 );
}
该函数可用于CSS版本控制,方法是附加上次更改的日期(使用filemtime
) 作为对CSS调用的查询字符串。你可以看到$info
和$show
作为传递给该函数的变量。但是我怎么知道这些变量包含什么呢?他甚至在条件逻辑中使用这些变量(\'stylesheet_url\' == $show
) 所以某种程度上,有些东西必须自动传递?