ホームページ > CMS チュートリアル > &#&プレス > 言及機能でWordPressコメントを強化します

言及機能でWordPressコメントを強化します

Jennifer Aniston
リリース: 2025-02-10 11:17:11
オリジナル
345 人が閲覧しました

このチュートリアルは、コメントにTwitterのような@mention機能を追加するWordPressプラグインを構築することを示しています。 ユーザーは互いにタグを付けて、コメントの相互作用を改善できます

Enhance Your WordPress Comments with a Mention Feature

主要な機能:

    @mention機能:
  • プラグインを使用すると、ユーザーはTwitterに似た「@」シンボルを使用して他のコメント者にタグを付けることができます。 電子メール通知:
  • 言及されたユーザーは、新しいコメントに関する電子メールアラートを受け取ります。
  • wordpress統合:WordPressのコメントモデレーションシステムとシームレスに統合
  • カスタマイズ:@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この強化されたバージョンは、コメントテキスト内で言及を直接スタイリングし、よりユーザーフレンドリーなエクスペリエンスを提供します。 電子メール通知も明確に改善されています。 生産環境で潜在的なエラー(たとえば、電子メールが見つかりません)を処理することを忘れないでください。

以上が言及機能でWordPressコメントを強化しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート