LoginSign Up

Comments with a Mention Feature in wordpress

প্রোগ্রামিং 8 months ago 8 Apr, 2020 at 2:49 am 655
Linkedin Pint

Many WordPress sites allow visitors to share comments on blog posts. There are several WordPress plugins that also enable visitors to leave a reply to comments. However, this process can get confusing when more than one author is replied to or referenced in the comment.

In this tutorial, I’ll be covering how to write a handy WordPress plugin that brings a Twitter-like mention feature right into our WordPress site, to allow readers to tag each other in comments.

At the end of this tutorial, I’ll provide a link to download the plugin which also has some additional features and a link to view the plugin on GitHub.

mention feature comments

Coding the Plugin
All WordPress plugins are PHP files located at /wp-content/plugins/ directory. In here, we create a PHP file wp-mention-plugin.php.

Next, we include the plugin header, otherwise WordPress won’t recognize our plugin.

$0“;
$mod_comment = preg_replace( $pattern, $replacement, $comment );

return $mod_comment;
}
Here, we create a method wpmp_mod_comment() meaning: WP Mention Plugin Modify Comment. The function accepts a single parameter, which is the comment we’re modifying. Don’t worry about this parameter, WordPress handles it.

The $pattern variable creates a simple regular expression pattern, which determines whether @any__comment__author__name is being mentioned in any comments. When things like admin@sitepoint are being included in a comment, they don’t get recorded as a valid mention. Valid mentions will be styled in a deep sky blue color.

If you would like to change the color of @mention text, just change the value of the $color_code variable.

Before we proceed further, let’s create the method that handles the notification email sent to the mentioned comment authors.

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 );

// we get the mentions here
$the_comment = $comment->comment_content;
$pattern = “/(^|\s)@(\w+)/”;

// if we find a match of the mention pattern
if ( preg_match_all( $pattern, $the_comment, $match ) ) {

// remove all @s from comment author names to effectively
// generate appropriate email addresses of authors mentioned
foreach ( $match as $m ) {
$email_owner_name = preg_replace( ‘/@/’, ”, $m );
$email_owner_name = array_map( ‘trim’, $email_owner_name );
}

/**
* This part is for For full names, make comment authors replace spaces them with
* two underscores. e.g, John Doe would be mentioned as @John__Doe
*
* PS: Mentions are case insensitive
*/
if ( preg_match_all( ‘/\w+__\w+/’, implode( ”, $email_owner_name ) ) ) {
$email_owner_name = str_ireplace( ‘__’, ‘ ‘, $email_owner_name );
}

// we generate all the mentioned comment author(s) email addresses
$author_emails = array_map( ‘self::wpmp_gen_email’, $email_owner_name );

// ensure at least one valid comment author is mentioned before sending email
if ( ! is_null( $author_emails ) ) {
$subj = ‘[‘ . get_bloginfo( ‘name’ ) . ‘] Someone mentioned you in a comment’;

$email_body = “Hello, someone mentioned you in a comment($the_related_comment_url)”.
” at MyBlog.com. Here is a link to the related post: $the_related_post_url”;

wp_mail( $author_emails, $subj, $email_body );

}
}
}
Code Explanation
In the above method, we filter the comment to check if anyone is being mentioned. And if true, we remove the preceeding ‘@’ symbol before the comment author name, so that we can generate the comment author info from the database using the name.

At times, comment authors do fill in full names, for example ‘John Doe’, or names with spaces as their comment author name. In cases like this, anyone mentioning them must replace spaces in the name with double underscores. So starting from the line if ( preg_match_all( ‘/\w+__\w+/’, implode( ”, $email_owner_name ) ) ) …. we ensure that all double underscores in @mention text gets converted into spaces.

We then generate the mentioned comment author email address using the wpmp_gen_email() method we’ll be creating next. The $email_body variable and the wp_mail() function stores and sends the notification email respectively, with a link to the corresponding comment and post.

NOTE If you noticed in the codes, mentioned comment authors and their email addresses are stored in an array, this is just in case more than one comment author is mentioned.

Remember to change the line MyBlog.com in $email_body to your desired site name.

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 );
$email_address = $wpdb->get_var( $prepare_email_address );

return $email_address;
}
Code Explanation
I’ve talked much on this method earlier, it simply generates the mentioned comment author email. The $name string parameter it accepts is the comment author name. Then it gets the mentioned comment author info from the database using the comment author name. To prevent an SQL attack, we use the WordPress prepare() function to ensure queries are safely handled.

Now we create two more methods which track whether a comment is first held for moderation or not, before sending the notification email.

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 );
}
The wpmp_approved() firstly checks if a comment with $comment_id held for moderation is approved, before calling the wpmp_send_mail() method which sends the notification email.

While wpmp_no_approve() calls the wpmp_send_mail() method which sends the email notification immediately when a comment is being automatically approved, without first holding for moderation.

Before the plugin can do what we’ve just coded it to do, we have to instantiate the class wp_mention_plugin we’ve just worked on, and call the initialize() method which registers our needed action and filter hooks.

}

$wp_mention_plugin = new wp_mention_plugin;
$wp_mention_plugin->initialize();

?>
plugin notification feature

Conclusion
In this tutorial, I covered how to create a handy WordPress plugin that brings a Twitter-like mention feature right into our WordPress site.

You can download Atmention in Comment here, a plugin I created that handles the mention feature integration with a few more features. Otherwise you can check out the plugin: Codex WP Mention Plugin code on GitHub.

Credit: Sitepoint

8 months ago

Abdullah sk
I,m article writer, part time job in kokilbd
Like - Dislike Votes 0 - Rating 0 of 10

6 Comments

    Abdullah sk
    April 8, 2020 at 3:00 am

    Thanks all guys

    Reply
    Abdullah sk
    April 10, 2020 at 12:30 pm

    function html_form_code() {
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Name (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Email (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Subject (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Message (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    }

    Reply
    Abdullah sk
    April 10, 2020 at 12:39 pm

    function deliver_mail() {

    // if the submit button is clicked, send the email
    if ( isset( $_POST[‘cf-submitted’] ) ) {

    // sanitize form values
    $name = sanitize_text_field( $_POST[“cf-name”] );
    $email = sanitize_email( $_POST[“cf-email”] );
    $subject = sanitize_text_field( $_POST[“cf-subject”] );
    $message = esc_textarea( $_POST[“cf-message”] );

    // get the blog administrator’s email address
    $to = get_option( ‘admin_email’ );

    $headers = “From: $name < $email>” . “\r\n”;

    // If email has been process for sending, display a success message
    if ( wp_mail( $to, $subject, $message, $headers ) ) {
    echo ‘

    ‘;
    echo ‘

    Thanks for contacting with us, expect a response soon.

    ‘;
    echo ‘

    ‘;
    } else {
    echo ‘

    An unexpected error occurred, please try again.

    ‘;
    }
    }
    }

    Reply
    Abdullah sk
    April 10, 2020 at 12:43 pm

    function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();

    return ob_get_clean();
    }

    Reply
    Abdullah sk
    April 10, 2020 at 12:43 pm

    add_shortcode( ‘kokilbd_contact_form’, ‘cf_shortcode’ );

    Reply
    Abdullah sk
    April 10, 2020 at 4:16 pm

    /*****kokilbd contact form start****/

    function html_form_code() {
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Name (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Email (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Subject (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘Your Message (required)
    ‘;
    echo ‘‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    echo ‘

    ‘;
    }

    function deliver_mail() {

    // if the submit button is clicked, send the email
    if ( isset( $_POST[‘cf-submitted’] ) ) {

    // sanitize form values
    $name = sanitize_text_field( $_POST[“cf-name”] );
    $email = sanitize_email( $_POST[“cf-email”] );
    $subject = sanitize_text_field( $_POST[“cf-subject”] );
    $message = esc_textarea( $_POST[“cf-message”] );

    // get the blog administrator’s email address
    $to = get_option( ‘admin_email’ );

    $headers = “From: $name < $email>” . “\r\n”;

    // If email has been process for sending, display a success message
    if ( wp_mail( $to, $subject, $message, $headers ) ) {
    echo ‘

    ‘;
    echo ‘

    Thanks for contacting with us, expect a response soon.

    ‘;
    echo ‘

    ‘;
    } else {
    echo ‘

    An unexpected error occurred, please try again.

    ‘;
    }
    }
    }

    function cf_shortcode() {
    ob_start();
    deliver_mail();
    html_form_code();

    return ob_get_clean();
    }
    add_shortcode( ‘kokilbd_contact_form’, ‘cf_shortcode’ );

    /**** kokilbd contact form****/

    Reply

Leave a Reply

Your email address will not be published.


*


Enter Captcha Here : *

Reload Image

     

আরও দেখুন

ফোরাম বিভাগ