• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Code Snippets » Filter emails for multiple languages

Filter emails for multiple languages

Chad Butler · Jul 3, 2019 ·

/**
 * Email locale settings function.
 *
 * This returns an array of locales to be used
 * by the action and filter functions for this
 * customization.  The default locale should be
 * whatever the site's default language locale
 * actually is.  Then set any locales for 
 * necessary additional languages.
 *
 * For a complete list of WordPress locales, see:
 * https://wpastra.com/docs/complete-list-wordpress-locale-codes/
 */ 
function my_wpmem_email_locales() {
    return array(
        
        'default' => 'en_US',
        
        'de_DE' => 'German', 
        'es_ES' => 'Spanish',
    );
}


add_action( 'wpmem_after_admin_init', 'my_add_custom_wpmem_email' );
/**
 * This function adds custom email messages for each additional 
 * language (locale). 
 *
 * While editing anything here isn't necessary, you may want 
 * to customize the text used in the custom email headings 
 * for the admin panel. That is noted in the code comments below.
 *
 * @uses wpmem_add_custom_email()
 * @see  https://rocketgeek.com/plugins/wp-members/docs/api-functions/wpmem_add_custom_email/
 */
function my_add_custom_wpmem_email() {
    
    // Get the settings array.
    $settings = my_wpmem_email_locales();
    
    /*
     * There isn't generally a reason to change the logic
     * in this section. However, text in the array could
     * be changed if different text is desired for the
     * custom email headings.
     */
    global $wpmem;
    if ( 1 == $wpmem->mod_reg ) {
        $email_type['newmod']  = __( 'Registration is Moderated', 'wp-members' );
        $email_type['appmod']  = __( 'Registration is Moderated, User is Approved', 'wp-members' );
    } else {
        $email_type['newreg']  = __( 'New Registration', 'wp-members' );
    }
    $email_type['repass']  = __( 'Password Reset', 'wp-members' );
    $email_type['getuser'] = __( 'Forgot Username', 'wp-members' );
    
    // If using the PayPal extension renewal reminder, uncomment the line below by removing "//"
    // $email_type['wpmem_exp_reminder'] = __( 'Subscription Renewal Reminder', 'wp-members' );
    
    // Remove the default locale from the array.
    unset( $settings['default'] );
    
    foreach ( $settings as $locale => $language ) {
        
        foreach ( $email_type as $tag => $email ) {
            // Create a custom email for each locale.
            $tag = 'localized_' . $tag . '_email_' . $locale;
            $heading = 'Custom email for ' . $email . ' [' . $language . ']';
            $subject_input = 'localized_' . $tag . '_email_subj_' . $locale;
            $message_input = 'localized_' . $tag . '_email_body_' . $locale;

            // Apply the arguments to the function.
            wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );
        }
    }
}


add_filter( 'wpmem_email_filter', 'apply_my_email_translation', 10, 3 );
/**
 * This function filters the email to the user based on the 
 * current site locale (language).
 */
function apply_my_email_translation( $email, $wpmem_fields, $field_data ) {
    
    // Get the settings array.
    $settings = my_wpmem_email_locales();
    
    // Get the locale.
    $locale = get_locale();
    
    // If the current locale is not the default locale, use custom language email.
    if ( $settings['default'] != $locale || '' == $locale || ! $locale ) {
    
        $localized_email = get_option( 'localized_' . $email['tag'] . '_email_' . $locale );
        
        if ( $localized_email ) {
            // Use custom values for subject and message body.
            $email['subj']       = $localized_email['subj'];
            $email['body']       = $localized_email['body'];
            $email['add_footer'] = false;
            $email['footer']     = '';
        }
    }
    
    return $email;
}

Not sure what to do with this code?

You're not a "coder" and don't know what to do? Don't worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.

Here are some free articles to get you started:

  • Using Code Snippets from the Site
  • Using a code snippets plugin
  • The functions.php File
  • Create a plugin file for custom functions
  • Create a child theme
  • Do not modify plugin files!

For "hands on" help, consider a plugin support subscription or the Pro Bundle.

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy