@@ -139,9 +139,14 @@ function get_settings_sections(): array {
139139 'general ' => __ ( 'General ' , 'code-snippets ' ),
140140 'editor ' => __ ( 'Code Editor ' , 'code-snippets ' ),
141141 'debug ' => __ ( 'Debug ' , 'code-snippets ' ),
142- 'version-switch ' => __ ( 'Version ' , 'code-snippets ' ),
143142 );
144143
144+ // Only show the Version section when the debug setting to enable version changes is enabled.
145+ $ enable_version = get_setting ( 'debug ' , 'enable_version_change ' );
146+ if ( $ enable_version ) {
147+ $ sections ['version-switch ' ] = __ ( 'Version ' , 'code-snippets ' );
148+ }
149+
145150 return apply_filters ( 'code_snippets_settings_sections ' , $ sections );
146151}
147152
@@ -169,8 +174,13 @@ function register_plugin_settings() {
169174 add_settings_section ( $ section_id , $ section_name , '__return_empty_string ' , 'code-snippets ' );
170175 }
171176
172- // Register settings fields.
177+ // Register settings fields. Only register fields for sections that exist (some sections may be gated by settings).
178+ $ registered_sections = get_settings_sections ();
173179 foreach ( get_settings_fields () as $ section_id => $ fields ) {
180+ if ( ! isset ( $ registered_sections [ $ section_id ] ) ) {
181+ continue ;
182+ }
183+
174184 foreach ( $ fields as $ field_id => $ field ) {
175185 $ field_object = new Setting_Field ( $ section_id , $ field_id , $ field );
176186 add_settings_field ( $ field_id , $ field ['name ' ], [ $ field_object , 'render ' ], 'code-snippets ' , $ section_id );
0 commit comments