如果我们看一下get_search_form()
, 请注意,在呈现窗体之前search_form_format
过滤器挂钩被触发。我们可以使用它添加另一个附加到get_search_form
其中回调取决于格式。
add_filter( \'search_form_format\', \'wpse_259716_search_form_format\', 99, 1 );
function wpse_259716_search_form_format( $format ) {
if( in_array( $format, array( \'xhtml\', \'html5\' ) ) ) {
add_filter( \'get_search_form\', "wpse_259716_get_search_form_$format", 99, 1 );
}
return $format;
}
function wpse_259716_get_search_form_xhtml( $form ) {
$search = \'<input type="submit"\';
$xhtml = \'some xhtml\';
$replace = $xhtml . $search;
return str_replace( $search, $replace, $form );
}
function wpse_259716_get_search_form_html5( $form ) {
$search = \'<input type="submit"\';
$html5 = \'some html5\';
$replace = $html5 . $search;
return str_replace( $search, $replace, $form );
}
或者,您可以使用基于类的方法。
$wpse_259716 = new wpse_259716();
add_filter( \'search_form_format\', array( $wpse_259716, \'search_form_format\' ), 99, 1 );
add_filter( \'get_search_form\', array( $wpse_259716, \'get_search_form\' ), 99, 1 );
class wpse_259716 {
protected $format;
public function search_form_format( $format ) {
return $this->format = $format;
}
public function get_search_form( $form ) {
$search = $replace = \'<input type="submit"\';
if( \'xhtml\' === $this->format ) {
$xhtml = \'some xhtml\';
$replace = $xhmtl . $search;
}
elseif( \'html5\' === $this->format ) {
$html5 = \'some html5\';
$replace = $html5 . $search;
}
return str_replace( $search, $replace, $form );
}
}