Logo

Logo

WordPress 创建自定义小工具

Pastore Antonio
Pastore Antonio 2024年04月19日
789 阅读 0 评论 约 6460 字 阅读约 13 分钟

之前介绍了如何让 WordPress 主题支持小工具的方法,默认的情况,只有不几个小工具,而且在国内这些小工具好多都不太常用。

如果你想给你的主题添加一个自定义小工具,或者你要开发一个添加小工具的插件,那就需要查看本文了。

 

创建小工具

创建一个小工具,需要使用 register_widget() 函数挂载一个继承于 WP_Widget 类的类,下边是一个简单的例子,创建了一个随机文章小工具。

注意,register_widget() 函数需要在 widgets_init 钩子上调用。

class widget_tags extends WP_Widget{
 
    //添加小工具
    function __construct(){
        $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) );
    }
 
    //小工具内容
    function widget( $args, $instance ){
 
        //导入当前侧边栏设置
        extract( $args, EXTR_SKIP );
 
        //输出小工具前代码
        echo $before_widget;
 
            //输出小工具标题
            echo $before_title . '随机文章' . $after_title;
 
            //随机文章
            query_posts( 'orderby=rand&showposts=10' );
            if( have_posts() ):
                echo '<ul>';
                    while( have_posts() ):
                        the_post();
                        printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() );
                    endwhile;
                echo '</ul>';
            endif;
 
        //输出小工具后代码
        echo $after_widget;
    }
 
}
function Bing_add_widget_tags(){
    register_widget( 'widget_tags' );
}
add_action( 'widgets_init', 'Bing_add_widget_tags' );

这样,后台就会出现了一个随机文章小工具,拖动到侧边栏上,会在前台显示十篇随机文章。

小工具设置

但我们会发现这个小工具并没有设置选项,那如何给这个小工具添加设置选项呢?设置选项涉及类的两个函数,update()(更新小工具设置时的处理函数)和 form()(设置表单)。

下边的代码添加了一个标题设置和显示文章数量的设置:

class widget_tags extends WP_Widget{
 
    //添加小工具
    function __construct(){
        $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) );
    }
 
    //小工具内容
    function widget( $args, $instance ){
 
        //导入当前侧边栏设置
        extract( $args, EXTR_SKIP );
 
        //输出小工具前代码
        echo $before_widget;
 
            //输出小工具标题
            echo $before_title . '随机文章' . $after_title;
 
            //随机文章
            query_posts( 'orderby=rand&showposts=10' );
            if( have_posts() ):
                echo '<ul>';
                    while( have_posts() ):
                        the_post();
                        printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() );
                    endwhile;
                echo '</ul>';
            endif;
 
        //输出小工具后代码
        echo $after_widget;
    }
 
    //更新选项
    function update( $instance, $old_instance ){
        $instance['title'] = strip_tags( $instance['title'] );
        $instance['number'] = (int) strip_tags( $instance['number'] );
        return $instance;
    }
 
    //选项表单
    function form( $instance ){
 
        //添加默认设置
        $instance = wp_parse_args( $instance, array(
            'title' => __( '随机文章', 'Bing' ),
            'number' => 10
        ) );
 
        //设置表单
?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( '标题', 'Bing' ); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( '文章数量', 'Bing' ); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" value="<?php echo $instance['number']; ?>" />
        </p>
<?php
    }
 
}
function Bing_add_widget_tags(){
    register_widget( 'widget_tags' );
}
add_action( 'widgets_init', 'Bing_add_widget_tags' );

这样再展开小工具,就能看到设置了:

然后,在生成小工具内容的时候使用选项,就能达到用户自定义的效果。

class widget_tags extends WP_Widget{
 
    //添加小工具
    function __construct(){
        $this->WP_Widget( 'random_posts', __( '随机文章', 'Bing' ), array( 'description' => __( '显示几篇随机文章', 'Bing' ) ) );
    }
 
    //小工具内容
    function widget( $args, $instance ){
 
        //导入当前侧边栏设置
        extract( $args, EXTR_SKIP );
 
        //添加小工具标题过滤器
        $title = apply_filters( 'widget_name', $instance['title'] );
 
        //输出小工具前代码
        echo $before_widget;
 
            //输出小工具标题
            echo $before_title . $title . $after_title;
 
            //随机文章
            query_posts( 'orderby=rand&showposts=' . $instance['number'] );
            if( have_posts() ):
                echo '<ul>';
                    while( have_posts() ):
                        the_post();
                        printf( '<li><a href="%s" title="%s">%s</a></li>', get_permalink(), get_the_title(), get_the_title() );
                    endwhile;
                echo '</ul>';
            endif;
 
        //输出小工具后代码
        echo $after_widget;
    }
 
    //更新选项
    function update( $instance, $old_instance ){
        $instance['title'] = strip_tags( $instance['title'] );
        $instance['number'] = (int) strip_tags( $instance['number'] );
        return $instance;
    }
 
    //选项表单
    function form( $instance ){
 
        //添加默认设置
        $instance = wp_parse_args( $instance, array(
            'title' => __( '随机文章', 'Bing' ),
            'number' => 10
        ) );
 
        //设置表单
?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( '标题', 'Bing' ); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( '文章数量', 'Bing' ); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" value="<?php echo $instance['number']; ?>" />
        </p>
<?php
    }
 
}
function Bing_add_widget_tags(){
    register_widget( 'widget_tags' );
}
add_action( 'widgets_init', 'Bing_add_widget_tags' );

 

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它

附件下载

共 2 个文件
loading
GIF 40.1 KB
470
PNG 11.1 KB
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

探索AIGC相关的精彩内容,共 15 篇文章

Azure AI 服务之语音识别

简介 Azure AI 服务中的语音识别 API 是微软提供的一项先进技术,旨在帮助开发者轻松实现语 ... WordPress 创建自定义小工具

2026-02-17 · Xzavier Aaron
MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... WordPress 创建自定义小工具

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... WordPress 创建自定义小工具

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... WordPress 创建自定义小工具

2026-02-14 · Xzavier Aaron