By 5tefan

2019-04-10 22:56:12 8 Comments

I'm currently rewriting a little plugin that shows visitors a small info text. This plugin is German only so far and so I want to add multi language support as I am working on a page that should be in English and German. The text that is displayed to the visitor can be set in the settings interface of the plugin. The given values are stored in a single option in the database. In my modified plugin I store the settings in separate options, each for one language. I've added a $_GET paramter which is used in the setting registration to create a unique name for a language specific option.

My important (stripped-down) parts of my plugin class:

class Privacy_Policies {
    // the language to which the text is translated
    public $translation;
    // the options, stored in the database
    public $options;

    public function __construct() {
        // use the current user locale as a fallback
        $this->translation = isset($_GET['lang']) ? $_GET['lang'] : get_user_locale();
        // if an option already exists, edit it
        // use $this->translation to determine the right language
        $this->options = get_option('privacy_policies_' . $this->translation);
        // call registration when settings page gets loaded
        add_action('admin_init', array($this, 'register_settings');
        // create the settings page
        add_action('admin_menu', array($this, 'add_settings_page');

    // settings validation, called as callback to register settings
    public function validate_settings($value) {
        if (isset($value['checkbox']) && 1 == $value['checkbox']) {
            $value[ 'checkbox' ] = 1;
        } else {
            $value[ 'checkbox' ] = 0;
        $value['notice'] = normalize_whitespace($value['notice' ]);
        $value['error_message'] = normalize_whitespace($value['error_message']);
        $value['style'] = normalize_whitespace($value['style']);

        return $value;

    // register a distinct option name for each language
    // the function is called when the settings page gets loaded
    public function register_settings() {
            'privacy_notice_settings_' . $this->translation,
            array( $this, 'validate_settings' )

    // create the html for the settings page
    // stripped down to a simple checkbox
    public function get_settings_page() { ?>
        <h2><?php echo $this->get_plugin_data('Name'); ?></h2>
        <form method="post" action="options.php">
            <?php settings_fields('privacy_notice_settings_group'); ?>
            <table class="form-table">
                <tr valign="top">
                        <!-- important: 'name' must reflect the option name! -->
                        <input type="checkbox" id="privacy_checkbox" name="privacy_notice_settings_<?php echo $this->translation ?>[checkbox]" value="1"
                        <?php if (isset($this->options['checkbox'])) {
                            checked('1', $this->options['checkbox']);
                        } ?> />
    <?php }

    // add settings page
    // called in the admin_menu hook
    public function add_settings_page() {
            'Privacy Policies Settings',
            'Privacy Policies',
            array( $this, 'get_settings_page' )

The funny thing is that, whatever locale is in $this->locale the setting will always be stored in the database as setting of WP's current locale (get_user_locale()). I.e. a setting's name that has been composed of 'privacy_notice_settings_' . $this->translation gets mysteriously recomposed to 'privacy_notice_settings_' . get_user_locale(). I suspect the secret lies somewhere burried in options.php which the form is calling. But I seem to be unable to debug this.


@5tefan 2019-04-14 12:12:35

I haven't yet tried to finally fix my issue but dare to say what's wrong: WP doesn't allow me to use $_GET directly. It expects these variables to get sanitized by first whitelisting them through the query_vars hook and later calling them through a WP_query instance - haven't figured out in detail yet but that seems to make sense to me. Here's a post on the wordpress forum that might explain more: query-string-url-parameter-in-wp-admin-custom-page

@majick 2019-04-16 14:09:09

well it may not be the "wordpress way", but there is actually nothing in wordpress to prevent you using $_GET key values directly.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Using the same class across multiple functions

  • 2019-05-06 05:15:42
  • Ryszard JÄ™draszyk
  • 28 View
  • 0 Score
  • 1 Answer
  • Tags:   plugin-development

1 Answered Questions

[SOLVED] Activate plugin button do nothing

1 Answered Questions

[SOLVED] Set language per post

1 Answered Questions

[SOLVED] How to change <html lang="bg-BG" , but only for one page

1 Answered Questions

1 Answered Questions

[SOLVED] Were the language toggles removed from WP recently?

1 Answered Questions

[SOLVED] Default Wordpress settings API data sanitization

1 Answered Questions

1 Answered Questions

[SOLVED] Multiple options pages validation for a plugin

Sponsored Content