wordpress小工具
创建一个类文件
用banner小工具为例:
(1) 创建一个bnner.php类文件 继承WP_Widget类,改类文件中包含三个函数:
1.定义小工具函数(函数名与类名同名)
2.小工具前端显示函数(函数名widget)
3.后台小工具编辑函数(函数名form)
(2)添加钩子
add_action( 'widgets_init', 'd_searchs' );
function d_searchs() {
register_widget( 'd_search' );
}
(3)具体代码如下
<?php
add_action( 'widgets_init', 'd_searchs' );
function d_searchs() {
register_widget( 'd_search' );
}
class d_search extends WP_Widget {
function d_search() {
$widget_ops = array( 'classname' => 'd_search', 'description' => '搜索' );
$this->WP_Widget( 'd_search', 'D-搜索', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
echo '<div class="d_banner_inner">';
?>
<div style="padding-top:10px;padding-left:10px;">
<form method="get" class="dropdown search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>" >
<input class="search-input" name="s" type="text" placeholder="输入关键字搜索"<?php if( is_search() ){ echo ' value="'.$s.'"'; } ?> autofocus="" x-webkit-speech=""><input class="btn btn-success search-submit" type="submit" value="搜索">
<ul class="dropdown-menu search-suggest"></ul>
</form>
</div>
<?php
echo '</div>';
echo $after_widget;
}
function form($instance) {
?>
<p>
<label>无需设置</label>
</p>
<?php
}
}
?>