askupa-software/amarkal-widget

基于Amarkal UI开发WordPress小工具

dev-master 2017-12-25 21:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:09:32 UTC


README

基于Amarkal UI开发WordPress小工具。

测试至: WordPress 4.7
依赖关系: amarkal-ui

amarkal-widget

概述

amarkal-widget 允许您使用 amarkal-ui 开发WordPress主题或插件的小工具。 amarkal-widget 负责构建管理用户表单和保存用户输入,这样您就可以专注于构建小工具本身了!

安装

通过Composer

如果您正在使用命令行

$ composer require askupa-software/amarkal-widget:dev-master

或者只需将以下内容添加到您的 composer.json 文件中

"require": {
    "askupa-software/amarkal-widget": "dev-master"
}

然后运行命令

$ composer install

这将把包安装到目录 vendors/askupa-software/amarkal-widget 中。现在您只需要包含composer自动加载器。

require_once 'path/to/vendor/autoload.php';

手动

从github下载 amarkal-uiamarkal-widget,并将它们包含到您的项目中。

require_once 'path/to/amarkal-ui/bootstrap.php';
require_once 'path/to/amarkal-widget/bootstrap.php';

使用方法

创建一个继承自 \Amarkal\Widget\AbstractWidget 的类。这个类应该实现2个方法

  • config() - 一个公共函数,返回一个包含小工具配置的数组。
  • widget( $args, $instance ) - 一个公共函数,用于打印小工具的前端HTML。

配置参数

示例代码

为您的小工具创建一个类

class MyCoolWidget 
extends \Amarkal\Widget\AbstractWidget
{   
    /**
     * The widget's configuration
     */
    public function config()
    {
        return array(
            'id'              => 'm-cool-widget',   // The widget's id
            'name'            => 'My Cool Widget',  // The widget's id
            'widget_options'  => array(
                'description' => 'Just a very very cool widget...'  // The widget's description
            ),
            'control_options' => array(),           // Optional
            
            /**
             * The 'fields' argument specifies a list of amarkal-ui components to be used for the widget's admin form.
             */
            'fields'          => array(
                array(
                    'name'          => 'title',
                    'title'         => __( 'Title:', 'slug' ),
                    'default'       => 'My Cool Widget',
                    'description'   => 'Specifies the widget\'s title',
                    'type'          => 'text'
                ),
                array(
                    'name'          => 'content',
                    'title'         => __( 'Text:', 'slug' ),
                    'default'       => 'My cool widget content',
                    'description'   => 'Specifies the widget\'s content',
                    'type'          => 'text'
                ),
            )
        );
    }
    
    /**
     * The front-end display of widget. User data is accesible through the $instance variable.
     */
    public function widget( $args, $instance ) 
    {
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );        
        
        echo $args['before_widget'];
        
        // Echo the widget's title if not empty
        if ( $title ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        
        // Echo the widget's content if not empty
        if( $instance['content'] ) {
            echo '<p>'.$instance['content'].'</p>';
        }
        
        echo $args['after_widget'];
    }
}

然后像注册其他小工具一样注册小工具

function register_widgets() {
    register_widget( 'MyCoolWidget' );
}
add_action( 'widgets_init', 'register_widgets' );