Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Translate Membership Level Names and Descriptions with Paid Memberships Pro
<?php
/*
* Filter membership level names and descriptions for translating.
*
* Add this code to a custom plugin or your active theme's functions.php file.
* Be sure to update the $pmpro_translated_levels array. Add a sub array for each locale.
* The sub array keys should be the membership level ids,
* and values should be an array with the name and description to translate to.
*
*/
// Defining translated levels.
global $pmpro_translated_levels;
$pmpro_translated_levels['es_ES'] = array(
1 => array(
'name' => 'Primero Nivel',
'description' => 'La descripción del nivel.',
'confirmation' => 'Mensaje de confirmación',
),
2 => array(
'name' => 'Segundo Nivel',
'description' => 'La descripción del nivel.',
'confirmation' => 'Mensaje de confirmación.',
),
);
// Filter $pmpro_levels global.
function my_init_membership_level_translate() {
global $pmpro_translated_levels;
if ( empty( $pmpro_translated_levels ) ) {
return;
}
$site_locale = get_locale();
foreach ( $pmpro_translated_levels as $locale => $localized_levels ) {
if ( $locale == $site_locale ) {
global $pmpro_levels;
$pmpro_levels = pmpro_getAllLevels( true, true );
// Translate
foreach ( $localized_levels as $level_id => $localized_level ) {
if ( ! empty( $pmpro_levels[$level_id] ) ) {
foreach ( $localized_level as $key => $value ) {
$pmpro_levels[$level_id]->$key = $value;
}
}
}
}
}
}
add_action( 'init', 'my_init_membership_level_translate', 1 );
// Filter levels page.
function my_pmpro_levels_array( $levels ) {
global $pmpro_translated_levels;
if ( empty( $pmpro_translated_levels ) ) {
return;
}
$site_locale = get_locale();
foreach ( $pmpro_translated_levels as $locale => $localized_levels ) {
if ( $locale == $site_locale ) {
// Translate
foreach ( $localized_levels as $level_id => $localized_level ) {
if ( ! empty( $levels[$level_id] ) ) {
foreach ( $localized_level as $key => $value ) {
$levels[$level_id]->$key = $value;
}
}
}
}
}
return $levels;
}
add_filter( 'pmpro_levels_array', 'my_pmpro_levels_array' );
// Filter checkout level
function my_pmpro_checkout_level_translate( $level ) {
global $pmpro_translated_levels;
if ( empty( $pmpro_translated_levels ) ) {
return $level;
}
$site_locale = get_locale();
foreach ( $pmpro_translated_levels as $locale => $localized_levels ) {
if ( $locale == $site_locale ) {
if ( ! empty( $localized_levels[$level->id] ) ) {
foreach ( $localized_levels[$level->id] as $key => $value ) {
$level->$key = $value;
}
}
}
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level_translate' );
// Fix current user's level.
function my_pmpro_after_set_current_user_translate() {
global $current_user, $pmpro_translated_levels;
if ( empty( $pmpro_translated_levels ) || empty( $current_user->membership_level ) ) {
return;
}
$site_locale = get_locale();
foreach ( $pmpro_translated_levels as $locale => $localized_levels ) {
if ( $locale == $site_locale ) {
if ( ! empty( $localized_levels[$current_user->membership_level->id] ) ) {
foreach ( $localized_levels[$level->id] as $key => $value ) {
$current_user->membership_level->$key = $value;
}
}
}
}
}
add_action( 'pmpro_after_set_current_user', 'my_pmpro_after_set_current_user_translate' );
@laurenhagan0306

This comment has been minimized.

Copy link

@laurenhagan0306 laurenhagan0306 commented Apr 3, 2021

This recipe is included in the blog post on "Translate Your Membership Level Information with Paid Memberships Pro" at Paid Memberships Pro here: https://www.paidmembershipspro.com/translate-your-membership-level-names-with-paid-memberships-pro/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment