最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Customize comment notification e-mails with HTML

matteradmin9PV0评论

I want to customize the default content of notification e-mails i found the following codes. but i need to use HTML in my own content. would you please make it with HTML? i do not know enough about PHP. so please do not refer me to some pages. save me please. i need it really with HTML :

function wpd_comment_notification_text( $notify_message, $comment_id ){
// get the current comment and post data
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
// don't modify trackbacks or pingbacks
if( '' == $comment->comment_type ){
    // build the new message text
    $notify_message  = sprintf( __( 'A new reviewer wants to receive "%s"' ), $post->post_title ) . "\r\n";
    $notify_message .= sprintf( __('Author : %1$s'), $comment->comment_author ) . "\r\n";
    $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
    $notify_message .= sprintf( __('When you are logged, you can see details about the reviewer reputation here: %s'), get_comment_link( $comment_id ) ) . "\r\n";

    if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
        if ( EMPTY_TRASH_DAYS )
            $notify_message .= sprintf( __(''), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
        else
            $notify_message .= sprintf( __(''), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
        $notify_message .= sprintf( __(''), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
    }
}
// return the notification text
return $notify_message;
}
add_filter( 'comment_notification_text', 'wpd_comment_notification_text', 20, 2 );

I want to customize the default content of notification e-mails i found the following codes. but i need to use HTML in my own content. would you please make it with HTML? i do not know enough about PHP. so please do not refer me to some pages. save me please. i need it really with HTML :

function wpd_comment_notification_text( $notify_message, $comment_id ){
// get the current comment and post data
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
// don't modify trackbacks or pingbacks
if( '' == $comment->comment_type ){
    // build the new message text
    $notify_message  = sprintf( __( 'A new reviewer wants to receive "%s"' ), $post->post_title ) . "\r\n";
    $notify_message .= sprintf( __('Author : %1$s'), $comment->comment_author ) . "\r\n";
    $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
    $notify_message .= sprintf( __('When you are logged, you can see details about the reviewer reputation here: %s'), get_comment_link( $comment_id ) ) . "\r\n";

    if ( user_can( $post->post_author, 'edit_comment', $comment_id ) ) {
        if ( EMPTY_TRASH_DAYS )
            $notify_message .= sprintf( __(''), admin_url("comment.php?action=trash&c=$comment_id") ) . "\r\n";
        else
            $notify_message .= sprintf( __(''), admin_url("comment.php?action=delete&c=$comment_id") ) . "\r\n";
        $notify_message .= sprintf( __(''), admin_url("comment.php?action=spam&c=$comment_id") ) . "\r\n";
    }
}
// return the notification text
return $notify_message;
}
add_filter( 'comment_notification_text', 'wpd_comment_notification_text', 20, 2 );
Share Improve this question edited Nov 25, 2018 at 14:46 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Nov 25, 2018 at 14:37 sh.dehnavish.dehnavi 411 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I found out it my self. we should add the following code to the Functions.php ti be able to sent Email via HTML codes:

<?php
function wpse27856_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );
?>

and then for customize the email, we should do like bellow to use HTML code in it:

<?php
function wpd_comment_notification_text( $notify_message, $comment_id ){
// get the current comment and post data
$comment = get_comment( $comment_id );
$post = get_post( $comment->comment_post_ID );
// don't modify trackbacks or pingbacks
if( '' == $comment->comment_type ){
    // build the new message text
    $notify_message = '<h1 style="font-family: tahoma; direction: rtl; margin-bottom: 0px !important; background: red; font-size: 22px; color: white; border: 1px solid red; padding: 10px 4px; text-align: center;">New Comment To You</h1>';

    $notify_message .= '</h1>';
    $notify_message .= '<h2 style="font-family: tahoma; direction: rtl; font-weight: normal; margin-bottom: 0px !important; font-size: 14px; border: 1px solid #ccc; border-bottom: 1px solid white; padding: 10px 4px; text-align: center;"><strong>';
    $notify_message .= sprintf( __( 'New comment on your post "%s' ), $post->post_title );
    $notify_message .= '</strong></h2>';
}
// return the notification text
return $notify_message;
}
add_filter( 'comment_notification_text', 'wpd_comment_notification_text', 20, 2 );
?>

Thanks anyway :)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far