How to keep a variable in COOKIE or GET query string?

I'm working on adding a language variable (locale) in my custom WordPress theme. I made the translation working by adding ?l=en_US to the URL. I would like to add the parameter into COOKIE, such that the site will remember the choice.

Also, I would like to build a language switcher ( allow visitors to choose their own language ). How should I achieve it ?

I am able to get the locale by (and load the text domain):

add_action('after_setup_theme', 'DYtheme_setup');
add_filter('locale', 'DYtheme_localized');
function DYtheme_setup(){
    load_theme_textdomain('donnie2012', get_template_directory() . '/lang');
}
function DYtheme_localized($locale) {
    if (isset($_GET['l'])) {
        return $_GET['l'];
    }
    return $locale;
}

but how to make the language parameter into COOKIE also in URL ? I've looked into add_query_args but I'm not sure I'm in right track.

Topic textdomain l10n cookies Wordpress

Category Web


With honor to @Wyck's link, I managed to add a language variable by adding an action hook to init and set the locale into cookie using PHP's setcookie() function.

For details, visit: Setting custom cookies in Wordpress

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.