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();
} );
}
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.