I created WordPress module, which adds my custom field in general settings in WordPress Admin. However after submitting whole form in admin settings, I need to modify my field's value before saving to database. How can I do that?
I would prefer to modify my field's data before any default WordPress form validation. Any clues? So far I spent on this hours, and could not google anything.
My code:
Located in my_module
's main php file.
function my_module__section(){
$settings_page_display = 'general';
$section_id = 'my_module__section';
add_settings_section(
$section_id,
'Notification information',
'my_module__section_description',
$settings_page_display
);
$field_id = 'notification_text';
$field_label = 'Name of customer';
add_settings_field(
$field_id,
$field_label,
'my_module__settings_fields_render',
$settings_page_display,
$section_id,
[$field_id, 'text']
);
register_setting('general', $field_id, 'esc_attr');
}
function my_module__section_description() {
echo '<p>Some description</p>';
}
function my_module__settings_fields_render($args) {
$field_id = $args[0];
$field_type = $args[1];
$option = get_option($field_id);
echo '<input type="' . $field_type . '" id="'. $field_id .'" name="'. $field_id .'" value="' . $option . '" required>';
}
add_action('admin_init', 'my_module__section');
Note:
I am not searching for any frontend / javascript hacks - no exception.