I am working on some plugin in wordpress and I have following array and foreach loop with function in it.
Problem is that somehow I always getting $locale_key variable same as $code when $locale_key variable is inside function.
Please help.
$languages = array(
array('af', 'af', 'Afrikaans'),
array('ar', 'ar', 'اÙعربÙØ©', 'rtl'),
array('az', 'az', 'AzÉrbaycan'),
array('be', 'bel', 'ÐелаÑÑÑÐºÐ°Ñ Ð¼Ð¾Ð²Ð°'),
array('bg', 'bg_BG', 'бÑлгаÑÑки'),
array('bs', 'bs_BA', 'Bosanski'),
array('ca', 'ca', 'Català '),
array('cs', 'cs_CZ', 'ÄeÅ¡tina'));
$lang = $_SESSION['lang'];
foreach ($languages as $key => $value) {
$locale_key = $languages[$key][1];
$code = $languages[$key][0];
echo $locale_key; // Here i get for example "bs_BA"
add_shortcode( $code, function($atts, $content = null, $locale_key) {
global $lang;
echo $locale_key; // And then here i get "bs"
if ($lang == $locale_key) {
return $content;
}
});
}
Try:
When you use foreach($array as $key => $value) you can access the index via $key and the corresponding value via $value (even if that is an array too).
Try something like above, im sure you will find the solution.