Maison >Tutoriel CMS >WordPresse >Améliorez vos commentaires WordPress avec une fonction de mention

Améliorez vos commentaires WordPress avec une fonction de mention

Jennifer Aniston
Jennifer Anistonoriginal
2025-02-10 11:17:11375parcourir

Ce didacticiel montre la création d'un plugin WordPress qui ajoute une fonctionnalité @mention de type Twitter aux commentaires. Les utilisateurs peuvent taguer les uns les autres, améliorant l'interaction des commentaires.

Enhance Your WordPress Comments with a Mention Feature

Caractéristiques de clé:

  • Fonctionnalité @mention: Le plugin permet aux utilisateurs de marquer d'autres commentateurs en utilisant le symbole "@", similaire à Twitter.
  • Notifications par e-mail: Les utilisateurs mentionnés reçoivent des alertes par e-mail sur les nouveaux commentaires.
  • Intégration WordPress: s'intègre parfaitement au système de modération des commentaires de WordPress.
  • Personnalisation: Ajustez facilement la couleur du texte @mention et autres paramètres.

Développement du plugin:

Le plugin, wp-mention-plugin.php, réside dans le répertoire /wp-content/plugins/. L'en-tête du plugin est crucial pour la reconnaissance WordPress:

<code class="language-php"><?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
 */
?></code>

La fonctionnalité de base est encapsulée dans la classe wp_mention_plugin:

<code class="language-php">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();
?></code>

Le plugin utilise comment_text, wp_set_comment_status et wp_insert_comment crochets pour gérer les mentions et les notifications. N'oubliez pas de remplacer "MyBlog.com" par le nom de votre site dans le corps de messagerie.

Enhance Your WordPress Comments with a Mention Feature

Cette version améliorée style directement la mention dans le texte du commentaire, offrant une expérience plus conviviale. La notification par e-mail est également améliorée pour plus de clarté. N'oubliez pas de gérer les erreurs potentielles (par exemple, aucun e-mail trouvé) dans un environnement de production.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn