register_widget_control()
云策文档标注
概述
register_widget_control() 是一个已弃用的 WordPress 函数,用于注册小工具控制回调以自定义选项。它已被 wp_register_widget_control() 替代,自 2.8.0 版本起弃用。
关键要点
- 函数已弃用:自 WordPress 2.8.0 起,应使用 wp_register_widget_control() 替代。
- 参数处理:支持 $name 参数为数组,根据元素数量自动处理名称。
- 内部调用:函数内部编译参数后调用 wp_register_widget_control()。
- 参数说明:接受 $name(侧边栏 ID)、$control_callback(回调函数)、$width(宽度)、$height(高度)和 $params(额外参数)。
代码示例
function register_widget_control($name, $control_callback, $width = '', $height = '', ...$params) {
_deprecated_function( __FUNCTION__, '2.8.0', 'wp_register_widget_control()' );
// Compat.
if ( is_array( $name ) ) {
if ( count( $name ) === 3 ) {
$name = sprintf( $name[0], $name[2] );
} else {
$name = $name[0];
}
}
$id = sanitize_title( $name );
$options = array();
if ( ! empty( $width ) ) {
$options['width'] = $width;
}
if ( ! empty( $height ) ) {
$options['height'] = $height;
}
wp_register_widget_control( $id, $name, $control_callback, $options, ...$params );
}注意事项
- 弃用警告:使用此函数会触发 _deprecated_function() 警告,建议更新代码以避免兼容性问题。
- 参数兼容性:$name 参数可以是字符串或数组,数组处理逻辑需注意元素数量。
- 相关函数:涉及 sanitize_title() 和 wp_register_widget_control(),开发者应熟悉这些替代方案。
原文内容
Registers widget control callback for customizing options.
Description
Allows $name to be an array that accepts either three elements to grab the first element and the third for the name or just uses the first element of the array for the name.
Passes to wp_register_widget_control() after the argument list has been compiled.
See also
Parameters
$nameint|stringrequired-
Sidebar ID.
$control_callbackcallablerequired-
Widget control callback to display and process form.
$widthintrequired-
Widget width.
$heightintrequired-
Widget height.
$paramsmixedrequired-
Widget parameters.
Source
function register_widget_control($name, $control_callback, $width = '', $height = '', ...$params) {
_deprecated_function( __FUNCTION__, '2.8.0', 'wp_register_widget_control()' );
// Compat.
if ( is_array( $name ) ) {
if ( count( $name ) === 3 ) {
$name = sprintf( $name[0], $name[2] );
} else {
$name = $name[0];
}
}
$id = sanitize_title( $name );
$options = array();
if ( ! empty( $width ) ) {
$options['width'] = $width;
}
if ( ! empty( $height ) ) {
$options['height'] = $height;
}
wp_register_widget_control( $id, $name, $control_callback, $options, ...$params );
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Deprecated. Use wp_register_widget_control() |
| 2.2.0 | Introduced. |