Hi, if you are using qTranslate-X the subscriber language should be got by the language of the current site. So you don’t need to add the field.
Hello, unfortunately the subscriber language that is returned is 50% wrong.
So I would prefer that the subscriber chooses himself the correct language, also specially because the only two choices are FR and EN (and not german, japanese, etc)
Ok, now I understand. Here I wrote some code to add a custom language dropdown in form and this value overrides the default language.
You can add it inside functions.php of your theme or in a php file in wp-content/mu-plugins folder. Let us know if it works.
/**
* Add the field to form
*/
function custom_easymail_set_lang_field ( $fields ) {
$fields['cf_lang'] = array(
'humans_name' => __( "Language" ),
'sql_attr' => "VARCHAR(5) DEFAULT NULL",
'sql_key' => true,
'input_type' => "select",
'input_options' => array(
"" => '',
"en" => "English",
"fr" => "French",
),
'input_mandatory' => true,
'input_validation' => 'custom_easymail_cf_check_lang',
);
return $fields;
}
add_filter ( 'alo_easymail_newsletter_set_custom_fields', 'custom_easymail_set_lang_field' );
/**
* Language validation
*
* @param $data
* @return bool
*/
function custom_easymail_cf_check_lang ($data) {
if ( in_array( $data, [ 'en', 'fr' ] ) ) {
return true;
} else {
return false;
}
}
/**
* Update the language of the new subscriber
*/
function custom_easymail_update_subscriber_lang ( $subscriber, $user_id=false ) {
alo_em_update_subscriber_by_email ( $subscriber->email, [], $subscriber->active, $subscriber->cf_lang, false );
}
add_action ( 'alo_easymail_new_subscriber_added', 'custom_easymail_update_subscriber_lang', 10, 2 );
thanks!
just tried it and I had to replace with this
$langs = array(‘en’,’fr’);
if ( in_array( $data, $langs ) ) {
return true;
} else {
return false;
}
}
and also this line
alo_em_update_subscriber_by_email ( $subscriber->email, [], $subscriber->active, $subscriber->cf_lang, false );
to
$dummy = array();
alo_em_update_subscriber_by_email ( $subscriber->email, $dummy, $subscriber->active, $subscriber->cf_lang, false );
}
and that works
probably my version of php!
Yes, probably you are right. Thanks for the feedback.