Languages: English • ウィジェット API 日本語 中文(简体) • (Add your language)
本页包含 WordPress 小工具接口(Widgets API)的技术文档。 如果您是一位主题设计者、或者插件作者,希望创建一个有效的挂件,建议您阅读本文。本文假定您了解 PHP 脚本语言的基础语法。
所谓的小工具(widget)就是一个在被调用时会输出字符到标准输出的 PHP 函数。把 PHP 函数变成小工具,则需要注册。调用 WordPress 小工具接口所注册的 PHP 回调函数(PHP 文档中称之为伪类型)即可注册您的函数。如下:
register_sidebar_widget($callback);
WordPress 小工具接口部分的代码在 wp-includes/widgets.php 中。
|
|
注意: 建议您 不要用 这些以 "wp_" 开头的函数,因为可能在日后的版本中改变。这就是为什么我们使用 register_sidebar_widget() 替代wp_register_sidebar_widget()的原因。
从 2.8 开始,开发挂件的工作变得更加容易。你只需要继承标准的挂件类和它的几个函数就可以了。
基类中包含了关于一个有效的挂件必须继承函数的信息。
class My_Widget extends WP_Widget {
function My_Widget() {
// 挂件实例化
}
function widget($args, $instance) {
// 输出挂件内容
}
function update($new_instance, $old_instance) {
// 选项保存过程
}
function form($instance) {
// 在管理界面输出选项表单
}
}
register_widget('My_Widget');
下面的代码创建了一个名为FooWidget的挂件。此挂件拥有一个设置标题的表单。
/**
* FooWidget Class
*/
class FooWidget extends WP_Widget {
/** 构造函数 */
function FooWidget() {
parent::WP_Widget(false, $name = 'FooWidget');
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
?>
<?php echo $before_widget; ?>
<?php echo $before_title
. $instance['title']
. $after_title; ?>
Hello, World!
<?php echo $after_widget; ?>
<?php
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
return $new_instance;
}
/** @see WP_Widget::form */
function form($instance) {
$title = esc_attr($instance['title']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<?php
}
} // class FooWidget
接着,这个简单的挂件可以注册在钩子 widgets_init 上:
// 注册 FooWidget 挂件
add_action('widgets_init', create_function('', 'return register_widget("FooWidget");'));
就是这么简单。就自动获得了一个可重复选择的挂件。不再需要特别的调整了。
在 版本信息 中有更多的信息。
The Google Search widget which was included in the deprecated (i.e. not needed since WordPress 2.2) original widget plugin is commented within inches of its life, so consider that your tutorial. Additionally, there are a few guidelines to follow:
function widget_myuniquewidget($args) {
extract($args);
?>
<?php echo $before_widget; ?>
<?php echo $before_title
. 'My Unique Widget'
. $after_title; ?>
Hello, World!
<?php echo $after_widget; ?>
<?php
}
register_sidebar_widget('My Unique Widget',
'widget_myuniquewidget');
Important: To use the above in a plugin, wrap it with:
function widget_myuniquewidget_register() {
--the above goes here--
register_sidebar_widget('My Unique Widget','widget_myuniquewidget');}
add_action('init', widget_myuniquewidget_register);
register_widget_control($name, $callback [, $width [, $height ]] );
You have no idea how glad we are that you asked that. Here are a few ideas:
Widgets can be coded so that they can exist one time or they can exist multiple times. Wordpress is doing the work for you to instantiate your Widget multiple times if you follow some rules.