WordPress的基本空小部件插件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordPress的基本空小部件插件相关的知识,希望对你有一定的参考价值。

I use this as a starting point for widgets/plugins I develop.
  1. <?php
  2. /*
  3. Plugin Name: Empty Plugin (Widget)
  4. Plugin URI: http://trepmal.com/
  5. Description: Does nothing - testing only
  6. Author: Kailey Lampert
  7. Version: 0.0
  8. Author URI: http://kaileylampert.com/
  9. */
  10. /*
  11.   Copyright (C) 2010 Kailey Lampert
  12.  
  13.   This program is free software: you can redistribute it and/or modify
  14.   it under the terms of the GNU General Public License as published by
  15.   the Free Software Foundation, either version 3 of the License, or
  16.   (at your option) any later version.
  17.  
  18.   This program is distributed in the hope that it will be useful,
  19.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21.   GNU General Public License for more details.
  22.  
  23.   You should have received a copy of the GNU General Public License
  24.   along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26.  
  27. add_action( 'widgets_init', 'emptypluginwidget_load' ); /* Add our function to the widgets_init hook. */
  28. function emptypluginwidget_load() { register_widget( 'emptypluginwidget' ); } /* Function that registers our widget. */
  29.  
  30. class emptypluginwidget extends WP_Widget {
  31.  
  32. function emptypluginwidget() {
  33. $widget_ops = array( 'classname' => 'emptypluginwidget', 'description' => 'empty plugin widget' ); /* Widget settings. */
  34. $control_ops = array( 'width' => 450, 'height' => 250, 'id_base' => 'emptypluginwidget' ); /* Widget control settings. */
  35. $this->WP_Widget( 'emptypluginwidget', 'Empty Plugin Widget', $widget_ops, $control_ops ); /* Create the widget. */
  36. }
  37.  
  38. function widget( $args, $instance ) {
  39. extract( $args );
  40.  
  41. echo $before_widget; /* Before widget (defined by themes). */
  42.  
  43. if ( $instance['title'] ) echo $before_title . $instance['title'] . $after_title; /* Title of widget (before and after defined by themes). */
  44.  
  45. echo '<ul>';
  46. foreach($instance as $name=>$value) {
  47. if ($name == 'title') continue;
  48. echo '<li>' . $name . ': ' . $value . '</li>';
  49. }
  50. echo '</ul>';
  51.  
  52. echo $after_widget; /* After widget (defined by themes). */
  53. }
  54.  
  55. function update( $new_instance, $old_instance ) {
  56. $instance = $old_instance;
  57.  
  58. foreach($new_instance as $k=>$v) { $instance[$k] = strip_tags($v); } /* Strip tags (if needed) and update the widget settings. */
  59.  
  60. return $instance;
  61. }
  62.  
  63. function form( $instance ) {
  64.  
  65. /* Set up some default widget settings. */
  66.  
  67. $defaults = array('title' => 'Empty Plugin Widget', 'content' => 'hello world');
  68.  
  69. $instance = wp_parse_args( (array) $instance, $defaults );
  70.  
  71. echo '<p><label for="'.$this->get_field_id( 'title' ).'">'. 'Title'.'</label><br />
  72. <input type="text" id="'. $this->get_field_id( 'title' ).'" name="'.$this->get_field_name( 'title' ).'" value="'.$instance['title'].'" /></p>';
  73.  
  74. echo '<p><label for="'.$this->get_field_id( 'content' ).'">'. 'Content'.'</label><br />
  75. <input type="text" id="'. $this->get_field_id( 'content' ).'" name="'.$this->get_field_name( 'content' ).'" value="'.$instance['content'].'" /></p>';
  76.  
  77. } //end function
  78. } //end class
  79.  
  80. ?>

以上是关于WordPress的基本空小部件插件的主要内容,如果未能解决你的问题,请参考以下文章

WordPress - 代码片段插件

Wordpress - 将代码片段包含到布局的选定部分的插件

php EMPTY WIDGET用于在wordpress中创建单个小部件的插件代码。

WordPress:从小部件代码访问数据库

markdown 有用的WordPress插件片段

如何为 WordPress 插件/小部件开发设置 Netbeans 项目