Heim > CMS-Tutorial > WordDrücken Sie > Verbessern Sie Ihre WordPress -Kommentare mit einer Erwähnungsfunktion

Verbessern Sie Ihre WordPress -Kommentare mit einer Erwähnungsfunktion

Jennifer Aniston
Freigeben: 2025-02-10 11:17:11
Original
344 Leute haben es durchsucht

Dieses Tutorial zeigt, ein WordPress-Plugin zu erstellen, das Kommentaren eine Twitter-ähnliche @mention-Funktion hinzufügt. Benutzer können sich gegenseitig markieren und die Kommentarinteraktion verbessern.

Enhance Your WordPress Comments with a Mention Feature

Schlüsselmerkmale:

  • @mention -Funktionalität: Mit dem Plugin können Benutzer andere Kommentatoren mit dem Symbol "@" mit Twitter einsetzen.
  • .
  • E -Mail -Benachrichtigungen:
  • Erwähnte Benutzer erhalten E -Mail -Benachrichtigungen über neue Kommentare.
  • WordPress -Integration:
  • Integriert sich nahtlos in das Kommentar -Moderationssystem von WordPress.
  • Anpassung:
  • Passen Sie einfach die @mention -Textfarbe und andere Einstellungen an.

Plugin -Entwicklung:

wp-mention-plugin.php Das Plugin /wp-content/plugins/ liegt im Verzeichnis

. Der Plugin -Header ist entscheidend für die WordPress -Erkennung:
<?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
 */
?>
Nach dem Login kopieren

wp_mention_plugin Die Kernfunktionalität wird innerhalb der

-Klassel eingekapselt:
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();
?>
Nach dem Login kopieren

comment_text Das Plugin verwendet wp_set_comment_status, wp_insert_comment und "MyBlog.com" Hooks, um Erwähnungen und Benachrichtigungen zu verwalten. Denken Sie daran,

durch den Namen Ihrer Website im E -Mail -Körper zu ersetzen.

Enhance Your WordPress Comments with a Mention Feature

Diese verbesserte Version stilft direkt die Erwähnung im Kommentartext und bietet ein benutzerfreundlicheres Erlebnis. Die E -Mail -Benachrichtigung wird ebenfalls für Klarheit verbessert. Denken Sie daran, potenzielle Fehler (z. B. keine E -Mail gefunden) in einer Produktionsumgebung zu behandeln.

Das obige ist der detaillierte Inhalt vonVerbessern Sie Ihre WordPress -Kommentare mit einer Erwähnungsfunktion. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage