ernilambar / widget-helper
此包已被 弃用 且不再维护。未建议替代包。
WordPress 小工具助手
1.0.1
2020-06-11 10:05 UTC
Requires
- php: >=5.6
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-17 19:49:48 UTC
README
这是一个简单的助手库,可以帮助您快速轻松地创建 WordPress 小工具。
要求
- WordPress 5.1+。
- PHP 5.6+。
- 使用 Composer 管理PHP依赖。
安装
首先,您需要打开命令行工具,并将目录更改为您的主题文件夹。
cd path/to/wp-content/themes/<your-theme-name>
然后,使用 Composer 安装此包。
composer require ernilambar/widget-helper
假设您尚未在主题中包含 Composer 自动加载文件,并且将其作为主题包的一部分进行打包,您需要在主题的 functions.php
中添加以下代码来自动加载此包(以及其他包)。
Composer 自动加载文件将自动为您加载包,并使代码可用于使用。
if ( file_exists( get_parent_theme_file_path( 'vendor/autoload.php' ) ) ) { require_once( get_parent_theme_file_path( 'vendor/autoload.php' ) ); }
此包包含小工具字段所需的JS和CSS。您可以根据以下示例将资产入队。
用法
您可以通过扩展助手类来创建新的小工具。
use Nilambar\WidgetHelper\Helper; use Nilambar\WidgetHelper\Assets; /** * Load widget assets. * * @since 1.0.0 * * @param string $hook Hook name. */ function theme_slug_load_widget_assets( $hook ) { if ( 'widgets.php' === $hook ) { Assets::load_assets(); } } add_action( 'admin_enqueue_scripts', 'theme_slug_load_widget_assets' ); /** * Widget class. * * @since 1.0.0 */ class Theme_Hello_World extends Helper { /** * Constructor. * * @since 1.0.0 */ public function __construct() { $args['id'] = 'theme-hello-world'; $args['label'] = esc_html__( 'Hello World Widget', 'widget-helper' ); $args['widget'] = array( 'classname' => 'theme_hello_world', 'description' => esc_html__( 'Hello world widget', 'widget-helper' ), ); $args['fields'] = array( 'sample_title' => array( 'label' => esc_html__( 'Title:', 'widget-helper' ), 'type' => 'text', 'class' => 'widefat', ), 'sample_message' => array( 'label' => esc_html__( 'Message:', 'widget-helper' ), 'type' => 'textarea', 'class' => 'widefat', ), 'sample_image' => array( 'label' => esc_html__( 'Image:', 'widget-helper' ), 'type' => 'image', ), 'sample_color' => array( 'label' => esc_html__( 'Color:', 'widget-helper' ), 'type' => 'color', 'default' => '#DDDDDD', ), ); parent::create_widget( $args ); } /** * Echo the widget content. * * @since 1.0.0 * * @param array $args Display arguments. * @param array $instance Instance of the widget. */ public function widget( $args, $instance ) { // Fetch widget values. $values = $this->get_field_values( $instance ); $values['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $values['title'] ) ) { echo $args['before_title'] . esc_html( $values['title'] ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } // Display logic will be here. echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } /** * Register widget. * * @since 1.0.0 */ function theme_slug_custom_widgets_init() { register_widget( 'Theme_Hello_World' ); } add_action( 'widgets_init', 'theme_slug_custom_widgets_init' );
可用字段
- 文本
- URL
- 电子邮件
- 数字
- 复选框
- 单选按钮
- 单选按钮图片
- 颜色
- 选择
- 下拉页面
- 下拉分类法
- 图片
- 文本区域
- 标题
- 消息
- 分隔符
版权和许可证
本项目采用 MIT 许可。
2020 © Nilambar Sharma。