// Register the settings
function myplugin_register_settings() {
register_setting('general', 'google_api_settings', 'myplugin_sanitize_callback');
// Add a new section to the General Settings page
add_settings_section(
'google_api_settings_section',
'Google API Settings',
'myplugin_section_callback',
'general'
);
// Add fields to the new section
add_settings_field(
'google_api_secret_key',
'Secret Key',
'myplugin_secret_key_callback',
'general',
'google_api_settings_section'
);
add_settings_field(
'google_api_key',
'API Key',
'myplugin_api_key_callback',
'general',
'google_api_settings_section'
);
}
// Sanitize and validate input
function myplugin_sanitize_callback($input) {
// Sanitize the secret key and API key using sanitize_text_field
$sanitized_input['google_api_secret_key'] = sanitize_text_field($input['google_api_secret_key']);
$sanitized_input['google_api_key'] = sanitize_text_field($input['google_api_key']);
// Additional validation can be added if needed
return $sanitized_input;
}
// Section callback
function myplugin_section_callback() {
echo '<p>Enter your Google API settings below:</p>';
}
// Secret Key field callback
function myplugin_secret_key_callback() {
$value = get_option('google_api_settings')['google_api_secret_key'];
echo "<input type='text' name='google_api_settings[google_api_secret_key]' value='$value' />";
}
// API Key field callback
function myplugin_api_key_callback() {
$value = get_option('google_api_settings')['google_api_key'];
echo "<input type='text' name='google_api_settings[google_api_key]' value='$value' />";
}
// Hook into the settings API
add_action('admin_init', 'myplugin_register_settings');