Performance: Settings API
MEDIUM IMPACT
This affects the admin page load speed and responsiveness when managing settings in WordPress.
<?php add_action('admin_init', function() { register_setting('my_plugin_options', 'my_plugin_option'); add_settings_section('my_plugin_section', 'My Plugin Settings', null, 'my_plugin'); add_settings_field('my_plugin_option', 'Option', function() { $value = get_option('my_plugin_option'); echo '<input type="text" name="my_plugin_option" value="' . esc_attr($value) . '">'; }, 'my_plugin', 'my_plugin_section'); }); add_action('admin_menu', function() { add_options_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', function() { echo '<form method="post" action="options.php">'; settings_fields('my_plugin_options'); do_settings_sections('my_plugin'); submit_button(); echo '</form>'; }); }); ?>
<?php add_action('admin_menu', function() { add_options_page('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', function() { echo '<form method="post" action="options.php">'; settings_fields('my_plugin_options'); do_settings_sections('my_plugin'); echo '<input type="text" name="my_plugin_option" value="' . get_option('my_plugin_option') . '">'; submit_button(); echo '</form>'; }); }); register_setting('my_plugin_options', 'my_plugin_option'); ?>
| Pattern | Option Lookups | PHP Processing | HTML Output Complexity | Verdict |
|---|---|---|---|---|
| Manual form fields without Settings API | Multiple redundant | High due to repeated calls | Unstructured and verbose | [X] Bad |
| Proper use of Settings API | Batched and cached | Lower due to organized calls | Clean and minimal | [OK] Good |