The issue with the data saving lies in this area of the code (all three similar functions).
printf(
'<textarea name="%1$s[$2$s]" id="%3$s" rows="1" cols="30" class="code">%4$s</textarea>',
$args['option_name'],
$args['name'],
$args['label_for'],
$args['value']
);
Or more specifically it’s, this part [$2$s]
, which should be, [%2$s]
. I totally get it though, these specifiers can be fiddly and easy to make typos with, it took running the code to work out where the issue was, i didn’t spot it just glancing at the code.
Moving on..
RE: sanitize_text_field
, your code comments say:
// Strips any non a-z, A-Z, or 0-9 characters out of submitted data via sanitization.
The documentation however says:
Checks for invalid UTF-8,
Converts single < characters to entities
Strips all tags
Removes line breaks, tabs, and extra whitespace
Strips octets
You might wish to have a browse through the different sanitize functions and see which matches best with the character restrictions you want for your values.
Not sure about the shortcode atts use, wp_parse_args
might be more fitting (although they do very similar things). Setting defaults for options can be done at the time you call get_option
, fyi.
$default_values = array(
'elv_setting_merchant_id' => '0123456',
'elv_setting_api_user_id' => 'apiusername',
'elv_setting_api_key' => 'K38qxPvFQhZneeIuygOaqwFuP0vg3DHKN93qfwynyQGMMAtXvb03Ms0ytLeQ1hKL'
);
$data = get_option( $option_name, $default_values );
And i don’t know if you have plans to change the textarea functions later, but given the 3 textarea functions all do exactly the same and the args are passed into the same places, they could just be a single function (one function to manage instead of three).
function elv_render_settings_field( $args ) {
printf(
'<textarea name="%1$s[%2$s]" id="%3$s" rows="1" cols="30" class="code">%4$s</textarea>',
$args['option_name'],
$args['name'],
$args['label_for'],
$args['value']
);
}
And then update each add_settings_field
to use elv_render_settings_field
instead of elv_render_settings_field1
, 2, 3.. etc.