Question


I need to send 2 emails, when new comment is posted and when new comment is approved. Site is multilingual and uses qtranslate-x plugin.

Here is code, which sends email when comment is posted. In this case 'get_the_title($post->ID)' returns post title on one language as expected.

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


Here is code, which sends email when comment is approved. In this case 'get_the_title($post->ID)' returns full post title formated like this: '[:en]Title in English[:ru]Title in Russian'.

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


Is there a way to get post title on one language using locale?

Was it helpful?

Solution

I have written a function that will convert the qtranslate-x string into array of language and based on that you can use a particular language.

/**
 * 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;
    }
}

This function accepts qtranslate-x string ( eg: [:en]Title in English[:fr]Title in French ) and breaks it into array.

It will return an associative array with key as [en]=>Title in English and [fr]=>Title in French. This function can accept any qtranslate-x string in form of [:en] English [:fr] French [:ru] Russia

Hope this helps you out

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top