wordpress插件开发之小工具适配主题
在网上查到的都是直接输出$befor_widget和$after_widget,实际开发中并不管用,发现现在在$args中,具体代码如下:
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php /** * Created by PhpStorm. * User: mmx * Date: 2020/1/16 * Time: 16:54 */ // 使用 widgets_init 动作钩子来执行自定义的函数 add_action('widgets_init', 'wlkq_qqlogin_register_widgets'); // 注册小工具 function wlkq_qqlogin_register_widgets() { register_widget('wlkq_qqLogin'); } //使用 WP_Widget 类来创建小工具 class wlkq_qqLogin extends WP_Widget { //构造函数 public function __construct() { $widget_ops = array( 'classname' => 'wlkq_qqLogin', 'description' => 'QQ登录' ); $this->WP_Widget('QQ登录', 'QQ登录的小工具', $widget_ops); } //小工具管理界面 public function form($instance) { $defaults = array('title' => "QQ登录"); $instance = wp_parse_args((array)$instance, $defaults); $title = $instance['title']; ?> <p>标题: <input class="widefat" 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 = $old_instance; $instance['title'] = strip_tags(trim($new_instance['title'])); return $instance; } //显示小工具 public function widget($args, $instance) { extract($args); $qqLoginObj = new QQLogin(); $url = $qqLoginObj->getAuthorizationCode(); $entry = ""; if (is_user_logged_in()) { $this_user = wp_get_current_user(); if (get_user_meta($this_user->ID, 'qq_openid', true) == "") { $title = "绑定您的QQ,下次方便登录!"; $entry = "绑定QQ"; } else { return ""; } } else { $entry = "QQ扫码登录"; $title = $instance['title']; } echo " {$args['before_widget']} {$args['before_title']}$title{$args['after_title']} <div> <ul> <li><a href='{$url}'>{$entry}</a></li> </ul> <div style='text-align: right;font-size: 12px;margin-top: 12px'><a target='_blank' href='http://www.wuzhixiang.cn'>@未来可期制作</a></div> </div> {$args['after_widget']} "; } } |