WordPress基础教程

WordPress最新评论小工具插件:显示隐藏站长评论,显示评论数,自定义摘要长度

苏工 avatarBy 苏工
8
image of WordPress最新评论小工具插件:显示隐藏站长评论,显示评论数,自定义摘要长度

WordPress自带的近期评论插件无法显示评论内容,参考官网的小工具制作教程,自己仿照写了一个 — SuFob Recent Comments Display Plugin。

介绍

SuFob Recent Comments Display Plugin是一款WordPress最新评论显示小工具,通过这个小工具可以设置是否显示管理回复,设置摘要长度以及设置显示的评论数量。一个文件满足所需功能。

安装方法

  • 方法一(推荐)

下载打包好的插件包,在网站后台插件>安装插件,选择上传插件,手动上传安装并启用插件。

点击下载SuFob-Recent-Comments-Display-Plugin

  • 方法二

在/当前主题/includes/widgets目录下建立sufob_widget_recent_comments.php文件,将代码清单的代码粘贴保存。在主题的function.php内添加下面的代码即可。

require_once( dirname(__FILE__ ) . '/includes/widgets/sufob-widget-comments.php' );

安装成功后在网站后台:外观>小工具 下会新增一个名为[SuFob.Com]最新评论小工具的新工具。


代码清单

<?php
/*
*Plugin Name:  SuFob Recent Comments Display Plugin
*Plugin URI:   http://blog.sufob.com/wordpress-sufob-recent-comments-display-plugin/
*Description:  Wordpress最近的评论显示插件,通过此插件设置显示管理回复,设置摘录长度和显示的评论计数
*Version:      1.0.0
*Author:       SuFob.com
*Author URI:   https://sufob.com/
*License:      GPL2
*License URI:  https://www.gnu.org/licenses/gpl-2.0.html
*/

// 注册并加载小工具

function sufob_load_widget()
{
    register_widget('sufob_widget_recent_comments');
}

add_action('widgets_init', 'sufob_load_widget');
class SuFob_Widget_Recent_Comments extends WP_Widget

{
    /**
     * 初始化评论实例
     */
    static $cache_id = 'sufob_widget_recent_comments';
    static $cache_flag = 'widget';
    public

    function __construct()
    {
        $widget_ops = array(

            // 小工具的识别ID

            'classname' => 'sufob_widget_recent_comments',

            // 后台小工具的描述

            'description' => __('显示你网站的最新评论') ,
            'customize_selective_refresh' => true,
        );
        parent::__construct('sufob-recent-comments', __('[SuFob.Com]最新评论小工具') , $widget_ops);
        $this->alt_option_name = 'sufob_widget_recent_comments';
    }

    public

    function widget($args, $instance)
    {
        global $wpdb;
        $cache = wp_cache_get(self::$cache_id, self::$cache_flag);
        if (!is_array($cache))
        {
            $cache = array();
        }

        if (isset($cache[$args['widget_id']]))
        {
            echo $cache[$args['widget_id']];
            return;
        }

        if (!isset($args['widget_id'])) $args['widget_id'] = $this->id;
        extract($args);
        $output = '';
        $title = (!empty($instance['title'])) ? $instance['title'] : __('最新评论');
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);
        $number = (!empty($instance['number'])) ? absint($instance['number']) : 5;
        if (!$number) $number = 5;
        $excerpt = (!empty($instance['excerpt'])) ? absint($instance['excerpt']) : 35;
        if (!$excerpt) $excerpt = 35;
        /**
         * 参数过滤
         * 4.9.0 开始新增 `$instance` 参数
         */
        $exclude_post_author = empty($instance['show_post_author']) ? array(
            1
        ) : array();
        $comments = get_comments(apply_filters('widget_comments_args', array(
            'number' => $number,
            'post_author__not_in' => $exclude_post_author,
            'status' => 'approve',
            'post_status' => 'publish'
        ) , $instance));
        $output.= $args['before_widget'];
        if ($title)
        {
            $output.= $args['before_title'] . $title . $args['after_title'];
        }

        $output.= '<ul id="recentcomments">';
        if (is_array($comments) && $comments)
        {

            // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)

            $post_ids = array_unique(wp_list_pluck($comments, 'comment_post_ID'));
            _prime_post_caches($post_ids, strpos(get_option('permalink_structure') , '%category%') , false);
            foreach((array)$comments as $comment)
            {
                $content = apply_filters('get_comment_text', $comment->comment_content);
                $content = mb_strimwidth(strip_tags($content) , 0, $excerpt, '...', 'UTF-8');
                $content = convert_smilies($content);
                $output.= '<li class="comment">' . get_avatar($comment, 40);
                $output.= '<div class="sufob_comment">';
                $output.= '<strong><span class="comment_author">' . get_comment_author($comment) . '</span></strong> ';
                $output.= '<span class="post_title">在 《<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '" target="_blank">' . get_the_title($comment->comment_post_ID) . '</a>》 评论:</span>';
                $output.= '<div class="comment-content"><p class="last">' . $content . '</p></div>';
                $output.= '</li>';
            }
        }

        $output.= '</ul>';
        $output.= $args['after_widget'];
        echo $output;
        $cache[$args['widget_id']] = $output;
        wp_cache_set(self::$cache_id, $cache, self::$cache_flag);
    }

    public

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field($new_instance['title']);
        $instance['number'] = absint($new_instance['number']);
        $instance['show_post_author'] = !empty($new_instance['show_post_author']);
        $instance['excerpt'] = absint($new_instance['excerpt']);
        $this->delete_comment_cache();
        return $instance;
    }

    public

    function form($instance)
    {
        $title = isset($instance['title']) ? $instance['title'] : '';
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
        $show_post_author_check = (isset($instance['show_post_author']) && $instance['show_post_author'] === true) ? 'checked="checked"' : '';
        $excerpt = isset($instance['excerpt']) ? absint($instance['excerpt']) : 70;
?>
        <p><label for="<?php
        echo $this->get_field_id('title'); ?>"><?php
        _e('Title:'); ?></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 esc_attr($title); ?>" /></p>

        <p><label for="<?php
        echo $this->get_field_id('number'); ?>"><?php
        _e('输入最大显示评论数'); ?></label>
        <input class="tiny-text" id="<?php
        echo $this->get_field_id('number'); ?>" name="<?php
        echo $this->get_field_name('number'); ?>" type="number" step="1" min="1" value="<?php
        echo $number; ?>" size="3" /></p>

        <p><label for="<?php
        echo $this->get_field_id('show_post_author'); ?>"><?php
        _e('是否显示站长评论?'); ?></label>
        <input id="<?php
        echo $this->get_field_id('show_post_author'); ?>" name="<?php
        echo $this->get_field_name('show_post_author'); ?>" type="checkbox" <?php
        echo $show_post_author_check; ?> /></p>

        <p><label for="<?php
        echo $this->get_field_id('excerpt'); ?>"><?php
        _e('评论摘要长度'); ?></label>
        <input class="tiny-text" id="<?php
        echo $this->get_field_id('excerpt'); ?>" name="<?php
        echo $this->get_field_name('excerpt'); ?>" type="text" value="<?php
        echo $excerpt; ?>" /></p>

        <?php
    }

    /**
     * 刷新评论插件缓存
     */
    static
    function delete_comment_cache()
    {
        wp_cache_delete(self::$cache_id, self::$cache_flag);
    }
}
作者:

苏工

SEO专家兼数据分析师,具备深厚的编程知识。擅长利用Python和前端技术(JavaScript、HTML、CSS)开发优化策略,提升网站排名和用户体验。精通GA4和谷歌标签管理器,熟练进行数据分析和解读,以数据驱动决策,优化SEO成效。结合技术能力和市场洞察,为电子商务和在线品牌打造全方位的增长解决方案。

更多文章
Charlie Sue profile picture.