首頁 > CMS教程 > &#&按 > 通過提及功能增強您的WordPress評論

通過提及功能增強您的WordPress評論

Jennifer Aniston
發布: 2025-02-10 11:17:11
原創
344 人瀏覽過

>本教程演示了構建一個WordPress插件,該插件在註釋中添加了類似Twitter的@Mention功能。 用戶可以互相標記,改善註釋互動。

Enhance Your WordPress Comments with a Mention Feature

密鑰功能:

  • @mention功能:插件使用戶可以使用“@”符號標記其他評論者,類似於Twitter。
  • 電子郵件通知:提到的用戶收到有關新評論的電子郵件警報。
  • WordPress集成:與WordPress的評論Meneration System無縫集成。
  • 自定義:
  • 輕鬆調整@mention文本顏色和其他設置。
>插件開發:

插件,,位於

>目錄中。 插件標頭對於WordPress識別至關重要:

wp-mention-plugin.php /wp-content/plugins/核心功能被封裝在

>類中:>
<?php
/**
 * Plugin Name: WP Mention Plugin
 * Plugin URI: https://sitepoint.com
 * Description: Mention registered and unregistered comment authors.
 * Version: 1.0.0
 * Author: John Doe
 * Author URI: https://sitepoint.com
 * License: GPLv2
 */
?>
登入後複製

>插件使用wp_mention_plugin>,

class wp_mention_plugin {

    public static function initialize() {
        add_filter( 'comment_text', array( 'wp_mention_plugin', 'wpmp_mod_comment' ) );
        add_action( 'wp_set_comment_status', array( 'wp_mention_plugin', 'wpmp_approved' ), 10, 2 );
        add_action( 'wp_insert_comment', array( 'wp_mention_plugin', 'wpmp_no_approve' ), 10, 2 );
    }

    public static function wpmp_mod_comment( $comment ) {
        $color_code = '#00BFFF'; // Deep sky blue
        $pattern = "/(^|\s)@(\w+)/";
        $replacement = "<span style='color:$color_code;'>@</span>"; //Style the mention
        $mod_comment = preg_replace( $pattern, $replacement, $comment );
        return $mod_comment;
    }

    private static function wpmp_send_mail( $comment ) {
        $the_related_post = $comment->comment_post_ID;
        $the_related_comment = $comment->comment_ID;
        $the_related_post_url = get_permalink( $the_related_post );
        $the_related_comment_url = get_comment_link( $the_related_comment );

        $the_comment = $comment->comment_content;
        $pattern = "/(^|\s)@(\w+)/";

        if ( preg_match_all( $pattern, $the_comment, $match ) ) {
            foreach ( $match[2] as $m ) {
                $email_owner_name[] = preg_replace( '/@/', '', $m );
            }

            if ( preg_match_all( '/\w+__\w+/', implode( '', $email_owner_name ) ) ) {
                $email_owner_name = str_ireplace( '__', ' ', $email_owner_name );
            }

            $author_emails = array_map( 'self::wpmp_gen_email', $email_owner_name );

            if ( ! is_null( $author_emails ) ) {
                $subj = '[' . get_bloginfo( 'name' ) . '] You were mentioned in a comment!';
                $email_body = "You were mentioned in a comment!  See it here: $the_related_comment_url\n\nRelated Post: $the_related_post_url";
                wp_mail( $author_emails, $subj, $email_body );
            }
        }
    }

    public static function wpmp_gen_email( $name ) {
        global $wpdb;
        $name = sanitize_text_field( $name );
        $query = "SELECT comment_author_email FROM {$wpdb->comments} WHERE comment_author = %s";
        $prepare_email_address = $wpdb->prepare( $query, $name );
        return $wpdb->get_var( $prepare_email_address );
    }

    public static function wpmp_approved( $comment_id, $status ) {
        $comment = get_comment( $comment_id, OBJECT );
        ( $comment && $status == 'approve' ? self::wpmp_send_mail( $comment ) : null );
    }

    public static function wpmp_no_approve( $comment_id, $comment_object ) {
        ( wp_get_comment_status( $comment_id ) == 'approved' ? self::wpmp_send_mail( $comment_object ) : null );
    }
}

$wp_mention_plugin = new wp_mention_plugin;
$wp_mention_plugin->initialize();
?>
登入後複製
掛鉤來管理提及和通知。 切記在電子郵件主體中用網站的名稱替換

comment_text> wp_set_comment_statuswp_insert_comment "MyBlog.com"

這個增強的版本直接在評論文本中設計了提及,從而提供了更具用戶友好的體驗。 為了清楚起見,還改進了電子郵件通知。 請記住要處理生產環境中的潛在錯誤(例如,找不到電子郵件)。

>

以上是通過提及功能增強您的WordPress評論的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板