我需要在发布新评论和批准新评论时发送两封电子邮件。该网站是多语言的,并使用qtranslate-x插件
下面是代码,在发布评论时发送电子邮件。在本例中,“get\\u the\\u title($post->ID)”按预期返回一种语言的帖子标题。
function kvkoolitus_email_comment_posted( $comment_ID, $comment_approved ) {
if( 0 === $comment_approved ){
$comment = get_comment( $comment_ID );
$post = get_post( $comment->comment_post_ID );
$blog_mail = get_option(\'admin_email\');
$comment_text = $comment->comment_content;
$email = $comment->comment_author_email;
$to = $blog_mail;
$from = \'KVKoolituskeskus <\' . $blog_mail . \'>\';
$subject = "KV Koolitused - Registreeru koolitusele";
$message = \'<html><body>\';
$message .= \'<p>\' . get_the_title($post->ID) . \'</p>\';
$message .= \'</body></html>\';
$headers = "From: " . $from . "\\r\\n";
$headers .= "Reply-to: " . $from . "\\r\\n";
$headers .= "Content-Type: text/html; charset=UTF-8";
mail($to, $subject, $message, $headers);
}
}
add_action( \'comment_post\', \'kvkoolitus_email_comment_posted\', 10, 2 );
下面是代码,当评论获得批准时,它会发送电子邮件。在本例中,“get\\u the\\u title($post->ID)”返回格式如下的完整帖子标题:“[:en]英语标题[:ru]俄语标题”。
function kvkoolitus_email_comment_approved($new_status, $old_status, $comment) {
if($old_status != $new_status) {
if($new_status == \'approved\') {
$post = get_post( $comment->comment_post_ID );
$name = $comment->comment_author;
$comment_mail = $comment->comment_author_email;
$blog_mail = get_option(\'admin_email\');
$to = $blog_mail;
$from = \'KVKoolituskeskus <\' . $comment_mail . \'>\';
$subject = "Comment approved on - " . get_the_title($post->ID);
$message = \'<html><body>\';
$message .= \'<p>\'. get_the_title($post->ID) . \'</p>\';
$message .= \'</body></html>\';
$headers = "From: ".$from."\\r\\n";
$headers .= "Reply-to: ".$from."\\r\\n";
$headers .= "Content-Type: text/html; charset=UTF-8";
mail($to, $subject, $message, $headers);
}
}
}
add_action(\'transition_comment_status\', \'kvkoolitus_email_comment_approved\', 10, 3);
有没有一种方法可以使用语言环境获取一种语言的帖子标题?
最合适的回答,由SO网友:Aftab 整理而成
我编写了一个函数,可以将qtranslate-x字符串转换为语言数组,并基于此可以使用特定的语言。
/**
* This function breaks q-translate string into array per language.
*
* @param - {string}$content - the q-translate string Eg : [:en]Title in English[:fr]Title in French
* @return {array/bool} $lang - an associative array with language id as key and content as value for each language element consist or false if wrong string provided.
*/
function codession_qtranslatex_string( $content ) {
$total_lang = substr_count( $content, \'[:\' );
$lang = array();
$start_index = 0;
if ( $total_lang > 0 ) {
while( $total_lang-- ) {
// last language
if ( $total_lang == 0 ) {
$lang_code = substr( $content, $start_index + 2, 2 );
$lang[ $lang_code ] = substr( $content, $start_index + 5 );
break;
}
// find the occurance of "[" from start
$end_index = strpos( $content, \'[:\', $start_index + 5 );
$lang_code = substr( $content, $start_index + 2, 2 );
if ( $end_index ) {
$lang[ $lang_code ] = substr( $content, $start_index + 5, $end_index - $start_index - 5 );
$start_index = $end_index;
} else {
return false;
}
}
return $lang;
} else {
return false;
}
}
此函数接受qtranslate-x字符串(例如,英文为:[:en]Title,法文为:[:fr]Title),并将其拆分为数组。
它将返回一个关联数组,其中键为英文的[en]=>标题和法文的[fr]=>标题。此函数可以接受[:en]英语[:fr]法语[:ru]俄语形式的任何qtranslate-x字符串
希望这对你有帮助
谢谢