I have created an email function which sends custom emails to users.
This function retrieves some acf fields from the current language, but I tried to do it in a batch instead of getting the email templates just once instead of again for each user.
I wanted to do this by language code. The steps are
$eot_benchmark = date( 'U', strtotime( '+8 days', current_time( 'timestamp' ) ) );
$users = [];
// get active countries
$all_languages = apply_filters( 'wpml_active_languages', null, [] );
$hidden_languages = apply_filters( 'wpml_setting', [], 'hidden_languages' );
foreach ( $all_languages as $key => $values ) {
if ( ! array_key_exists( $key, $hidden_languages ) ) {
// get all users which have set $key as preferred language
$user_args = array(
'role__in' => array( 'independent', 'company' ),
'role__not_in' => array(),
'meta_query' => array(
array(
'key' => 'sd8_vip_eot_date',
'value' => $eot_benchmark,
'compare' => '<',
),
array(
'key' => 'sd8_vip_eot_date',
'value' => date( 'U', current_time( 'timestamp' ) ),
'compare' => '>',
),
array(
'key' => 'sd8_preferred_language',
'value' => $key,
'compare' => '=',
),
),
'number' => -1,
);
$users = get_users( $user_args );
if ( class_exists( 'SitePress' ) ) {
// if this language has any users who set $key as preferred language
if ( count( $users ) > 0 ) {
add_filter( 'acf/settings/current_language', function() {
return $key;
} );
}
}
// do email stuff
if ( class_exists( 'SitePress' ) ) {
add_filter( 'acf/settings/current_language', function () {
return ICL_LANGUAGE_CODE;
} );
}
}
}
4 is the one I am struggling with. I can’t get $key to return the correct language (code). I know this isn’t the proper way to do it, but it illustrates what I’d like to achieve.
I have a way to do it, but it isn’t the most ideal one, because it’s not DRY.
if ( 'nl' == $key ) {
add_filter( 'acf/settings/current_language', function() {
return 'nl';
} );
} elseif ( 'en' == $key ) {
add_filter( 'acf/settings/current_language', function() {
return 'en';
} );
} else {
add_filter( 'acf/settings/current_language', function () {
global $sitepress;
return $sitepress->get_default_language();
} );
}
The topic ‘Trying to return a user dependent language’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.