请写一个wordpress头像挂件的功能代码可以用户自行选择
function wp_avatar_widget() { //注册挂件 register_widget('wp_avatar');
//wp_avatar类
class wp_avatar extends WP_Widget {
//构造函数
function __construct() {
parent::__construct(
'wp_avatar', //挂件ID
__('头像挂件', 'wpb_widget_domain'), //挂件名称
array('description' => __('一个可以让用户选择头像的挂件', 'wpb_widget_domain'),) //挂件描述
);
}
//挂件输出
public function widget($args, $instance) {
//输出挂件前
echo $args['before_widget'];
//输出挂件标题
if (!empty($instance['title'])) {
echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
}
//输出挂件内容
echo '<div class="wp-avatar-widget">';
echo '<p>' . __('请选择您的头像:', 'wpb_widget_domain') . '</p>';
echo '<div class="wp-avatar-list">';
echo get_avatar(get_current_user_id(), 96, '', '', array('class' => 'wp-avatar-current')); //输出当前头像
echo '<ul>';
echo '<li><a href="#" data-avatar="1">' . get_avatar(get_current_user_id(), 48, '', '', array('class' => 'wp-avatar-preview')) . '</a></li>'; //输出预览头像1
echo '<li><a href="#" data-avatar="2">' . get_avatar(get_current_user_id(), 48, '', '', array('class' => 'wp-avatar-preview')) . '</a></li>'; //输出预览头像2
echo '<li><a href="#" data-avatar="3">' . get_avatar(get_current_user_id(), 48, '', '', array('class' => 'wp-avatar-preview')) . '</a></li>'; //输出预览头像3
echo '</ul>';
echo '</div>';
echo '<input type="hidden" name="wp-avatar-value" value="' . get_avatar(get_current_user_id(), 96, '', '', array('class' => 'wp-avatar-value', 'size' => 96)) . '">'; //输出隐藏域,用于提交表单
echo '</div>';
//输出挂件后
echo $args['after_widget'];
}
//挂件选项
public function form($instance) {
$title = !empty($instance['title']) ? $instance['title'] : __('新的头像', 'wpb_widget_domain');
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</p>
<?php
}
//更新挂件选项
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}
}
}
//加载挂件 add_action('widgets_init', 'wp_avatar_widget')
原文地址: https://www.cveoy.top/t/topic/hkTc 著作权归作者所有。请勿转载和采集!