Performance: Reading and writing settings
This concept affects page load speed and interaction responsiveness by how settings are accessed and saved in WordPress.
Jump into concepts and practice - no test required
<?php // Read once and cache in variable $setting = get_option('my_plugin_setting'); // Use $setting multiple times without extra calls // Write once after all changes update_option('my_plugin_setting', $final_value); ?>
<?php // Reading settings multiple times in a single request $setting1 = get_option('my_plugin_setting'); // ... some code ... $setting2 = get_option('my_plugin_setting'); // Writing settings multiple times update_option('my_plugin_setting', $new_value); update_option('my_plugin_setting', $new_value2); ?>
| Pattern | Database Queries | Writes | Blocking Time | Verdict |
|---|---|---|---|---|
| Multiple get_option/update_option calls | Multiple queries | Multiple writes | Blocks rendering 50-100ms | [X] Bad |
| Single get_option and one update_option | Single query | Single write | Minimal blocking | [OK] Good |
get_option is the WordPress function designed to retrieve saved settings. The others are not valid WordPress functions.get_option reads the saved option value, it is the correct choice.update_option, which takes two parameters: the option name and the new value.update_option('site_color', 'blue');. update_option('site_color' => 'blue'); uses an incorrect array syntax. get_option('site_color', 'blue'); uses get_option which reads, not writes. set_option('site_color', 'blue'); uses a non-existent function.$color = get_option('site_color', 'red');
echo $color;get_option returns the default value provided as the second argument, here 'red'.update_option('background_color');update_option requires two parameters: the option name and the new value to set.$size = intval($_POST['font_size']);
if ($size > 0) {
update_option('font_size', $size);
} uses intval to convert input to integer and checks if it is positive before saving.update_option, preventing invalid data.