Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.