Comment Notifications to a Per-Language Moderator
Sends comment moderation emails to a different moderator depending on the language of the post, using WPML, Polylang or each post's language box, so every comment reaches someone who reads it.
Opens WPIDE on
Code
$wpide_moderators = array( // Language code => moderator email.
'fr' => '[email protected]',
'de' => '[email protected]',
);
$wpide_post_lang = function ( $post_id ) {
if ( has_filter( 'wpml_post_language_details' ) ) {
$details = apply_filters( 'wpml_post_language_details', null, $post_id );
if ( is_array( $details ) && ! empty( $details['language_code'] ) ) {
return $details['language_code'];
}
}
if ( function_exists( 'pll_get_post_language' ) ) {
$lang = pll_get_post_language( $post_id );
if ( $lang ) {
return $lang;
}
}
$meta = get_post_meta( $post_id, '_wpide_lang', true );
return $meta ? strtolower( strtok( $meta, '-' ) ) : '';
};
add_filter( 'comment_moderation_recipients', function ( $emails, $comment_id ) use ( $wpide_moderators, $wpide_post_lang ) {
$comment = get_comment( $comment_id );
$lang = $comment ? $wpide_post_lang( (int) $comment->comment_post_ID ) : '';
return isset( $wpide_moderators[ $lang ] ) ? array( $wpide_moderators[ $lang ] ) : $emails;
}, 10, 2 );
Notes
Map language codes to email addresses; posts in other languages keep the default recipients.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.