underpin / widget-loader
Underpin 的小部件加载器
1.1.0
2021-11-23 16:42 UTC
Requires
- underpin/underpin: ^2.0
This package is auto-updated.
Last update: 2024-09-23 22:44:30 UTC
README
一个帮助向WordPress网站添加小部件的加载器。它使用Underpin的内置字段API来处理小部件数据的渲染和存储
安装
使用Composer
composer require underpin/widget-loader
手动安装
此插件使用内置的自动加载器,因此只要它在Underpin之前被要求,就应该按预期工作。
require_once(__DIR__ . '/underpin-widgets/index.php');
设置
- 安装Underpin。请参阅Underpin 文档
- 根据需要注册新的小部件菜单。
示例
一个非常基础的示例可能看起来像这样。
// Register widget underpin()->widgets()->add( 'hello-world-widget', [ 'name' => underpin()->__( 'Hello World Widget' ), // Required. The name of the widget. 'id_base' => 'widget_name', // Required. The ID. 'description' => underpin()->__( 'Displays hello to a specified name on your site.' ), // Widget description. 'widget_options' => [ // Options to pass to widget. See wp_register_sidebar_widget 'classname' => 'test_widget', ], 'get_fields_callback' => function ( $fields, \WP_Widget $widget ) { // Fetch, and set settings fields. $name = isset( $fields['name'] ) ? esc_html( $fields['name'] ) : 'world'; return [ new \Underpin\Factories\Settings_Fields\Text( $name, [ 'name' => $widget->get_field_name( 'name' ), // See WP_Widget get_field_name 'id' => $widget->get_field_id( 'name' ), // See WP_Widget get_field_id 'setting_key' => 'name', // Must match field name and field ID 'description' => underpin()->__( 'Optional. Specify the person to say hello to. Default "world".' ), 'label' => underpin()->__( 'Name' ), ] ), ]; }, 'display_callback' => function ( $instance, $fields ) { // Render output $name = ! empty( $fields['name'] ) ? esc_html( $fields['name'] ) : 'world'; echo underpin()->__( sprintf( 'Hello, %s!', $name ) ); }, ] );
或者,您可以扩展 Widget
并直接引用扩展的类,如下所示
underpin()->widgets()->add('widget-key','Namespace\To\Class');