-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GH-28 Refactor code to add setting fields from an array #32
base: main
Are you sure you want to change the base?
Conversation
foreach ( $script_settings as $option_name => $settings ) { | ||
|
||
add_settings_field( | ||
$settings[ 'id' ], | ||
$settings[ 'title' ], | ||
$settings[ 'callback' ], | ||
$settings[ 'page' ], | ||
$settings[ 'section' ] | ||
); | ||
|
||
register_setting( 'rt-scripts-optimizer-settings', $option_name ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @sudhir-yadav @pradeep910, This is the exact same code that we had earlier just shorten by adding a foreach loop. I think we need to register only 1 settings and fetch other settings by array key. Something similar to below code.
// Define default array
$defaults = array(
'setting_1' => 'default_value_1',
'setting_2' => 'default_value_2',
);
// Register the setting
register_setting( 'my_settings_group', 'my_array_setting', array(
'type' => 'array',
'default' => $defaults,
) );
// Update code that uses the setting
$options = get_option( 'my_array_setting', $defaults );
$setting_1 = $options['setting_1'];
$setting_2 = $options['setting_2'];
Of course we need to handle for the settings that are saved with standalone options after implementing above. This is my suggestion, and it would best because we need to call just one option and that would be stored in an array we can fetch by array key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also settings callback would be same, there would be no change, just option fetch from settings key.
This PR refactors the current settings to a single array of fields in one setting.