Encoding matter - ? instead of € after settings field callback

In my plugin I have som classes to manage settings field.

For some reason in one moment the setting field value with special characters like 30€ in textarea or input[type=text] change to be 30?.

I got the value like this in my class :

add_action( admin_init, array( $this, init_options ), 1 ); //$this is the Class

function init_options(){
  $this-current_options = get_option( $this-current_options_name );   
  $this-add_settings();
}

And before to add settings fields, the text is correctly encoded.

function add_settings(){
   ...
 $a_callback( ... ); //--Set this current object with a method
 if( $field_title === Terms and conditions ){
     var_dump( $a_callback ); //At this moment, I can see in the object, current_options containing special caracters like '€'
 }               

   add_settings_field(
       $field_id, // ID
       __($field_title, $this-current_plugin_domain), // Title
       $a_callback, // Callback
       PAGE_SETTINGS, // Page in constant
       $section_id,// Section
       $args_field //args
   );
   ...
}

For the callback, for some reasons, I´m using the magic method __call ( string $name , array $arguments ) :

public function __call( $name, $arguments ){
        if( preg_match(  /^(setting_section_)([a-z\-]*)(_callback)$/, $name ) ){
            $this-print_section_info();
        }else if( preg_match( /^([a-z\-_]*)(_callback)$/, $name ) ){
            foreach ( $arguments as $argument ){
                $args = [];
                $args['id']   = $argument['id'];
                $fieldType = $argument['type'];
                $args['section_slug']  = $argument['section_slug'];
                if( $args['id']  === terms-conditions ){
                    var_dump( $this-current_options['terms-conditions'] ); //here the char € was replaced by ?
                }
                ...
            }
        }
 }

So to get an overview to help a little more :

class Options{
    protected $current_options;
    public $options_names = array();
    public $options_group;
    public $current_options_name;
    
    public function __construct(...){
        add_action( admin_init, array( $this, init_options ), 1 );
    }
    public function init_options(){
        foreach ( $this-options_names as $options_name ){
            register_setting(
                $this-options_group, // Option group
                $options_name, // Option name
                array( __CLASS__, sanitize ) // Sanitize
            );
        }
        $this-set_current_options();
        $this-add_settings();
   }

   protected function set_current_options(){
       $this-current_options = get_option( $this-current_options_name );
   }

   protected function add_settings(){
       ...
       $a_callback( ... ); //--Set this current object with a method
       if( $field_title === Terms and conditions ){
          var_dump( $a_callback ); //At this moment, I can see in the object, current_options containing special caracters like '€'
       }               

       add_settings_field(
           $field_id, // ID
           __($field_title, $this-current_plugin_domain), // Title
           $a_callback, // Callback
           PAGE_SETTINGS, // Page in constant
           $section_id,// Section
           $args_field //args
       );
       ...
    }

    public function __call( $name, $arguments ){
        var_dump( $this-current_options['terms-conditions'] ); //text modified = special characters  modified by ?

    }
    
}

So, after the callback, there is a difference of encoding...

Important notices

  1. With the char $, I don´t have this problem, only with .
  2. if i do var_dump( get_option( $this-current_options_name ) ); after __call(), I have the same problem so it seems to be link with get_option()`

How can I resolve this ?

Topic page-specific-settings options Wordpress encoding

Category Web

About

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