Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

sudhir-yadav
Copy link

@sudhir-yadav sudhir-yadav commented Feb 3, 2023

This PR refactors the current settings to a single array of fields in one setting.

@sudhir-yadav sudhir-yadav changed the title GH-28 Refactor code to add setting fields from an array Feb 3, 2023
@sudhir-yadav sudhir-yadav changed the title [WIP]GH-28 Refactor code to add setting fields from an array Feb 3, 2023
@sudhir-yadav sudhir-yadav marked this pull request as ready for review February 3, 2023 10:44
Comment on lines +85 to +95
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 );

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants