My WordPress and PHP knowledge is limited, but I am working on a plugin, and in the admin page I have three settings - two text inputs and one checkbox.
On save, the values are stored in the wp_options table. If the options exist, the page then shows the values of the text inputs underneath the inputs. So far so good.
My question is how can submit the form, without overriding the wp_options with blank values if the text fields are blank when saved?
Use case: Allow users to update the checkbox (turn off/on) without the need to re-enter the text details.
<?php defined('ABSPATH') or die('This page may not be accessed directly.'); add_action('admin_menu', 'myplugin_admin_menu'); function myplugin_admin_menu() { add_menu_page('MyPlugin', 'MyPlugin', 'manage_options', 'myplugin-admin-page.php', 'myplugin_admin_page', 81); } add_action('admin_init', 'register_myplugin_settings'); function register_myplugin_settings() { register_setting('myplugin_settings_group', 'myplugin_id'); register_setting('myplugin_settings_group', 'myplugin_api_key'); register_setting('myplugin_settings_group', 'myplugin_checkbox'); } add_action('admin_print_styles', 'admin_styles'); function admin_styles() { wp_enqueue_style('style', plugins_url('myplugin-admin.css', __FILE__), array(), time()); } // Content for WP Admin Page function myplugin_admin_page() { ?> <header><img src="<?php echo plugins_url('/images/myplugin.svg', __FILE__); ?>"></header> <div class="mp-content"> <h2>Settings</h2> <form method="POST" action="<?php echo admin_url('options.php'); ?>"> <?php settings_fields('myplugin_settings_group'); ?> <?php do_settings_sections('myplugin_settings_group'); ?> <label for="appid">ID </label> <input type="text" id="appid" name="myplugin_id" /> <p> <?php $ID = esc_attr(get_option('myplugin_id')); if (!empty($ID)) { echo '✅ ' . $ID; } else { echo '❌ Please enter your ID'; } ?> </p> <label for="apikey">API Key </label> <input type="text" id="apikey" name="myplugin_api_key" /> <p> <?php $api = esc_attr(get_option('myplugin_api_key')); if (!empty($apiKey)) { // Get the last four characters $lastFour = substr($apiKey, -4); // Hide the rest of the key with asterisks $hiddenKey = str_repeat('*', strlen($apiKey) - 4) . $lastFour; echo '✅ ' . $hiddenKey; } else { echo '❌ Please enter your API Key'; } ?> </p> <div class="checkbox-wrapper"> <label for="checkbox-option"> <input id="checkbox-option" type="checkbox" name="myplugin_checkbox" <?php echo get_option('myplugin_checkbox') && get_option('myplugin_checkbox') == 'on' ? 'checked' : '' ?>> <span class="checkbox"></span> Checkbox option label </label> <div class="more-information" aria-label="More information"><svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="currentColor"> <g fill="currentColor"> <path d="M8 0a8 8 0 108 8 8.009 8.009 0 00-8-8zm0 12.667a1 1 0 110-2 1 1 0 010 2zm1.067-4.054a.667.667 0 00-.4.612.667.667 0 01-1.334 0 2 2 0 011.2-1.834A1.333 1.333 0 106.667 6.17a.667.667 0 01-1.334 0 2.667 2.667 0 113.734 2.444z"></path> </g> </svg> </div> </div> <?php submit_button(); ?> </form> </div> <?php }
I was searching Google for "ignore empty inputs on settings save", and similar. Which didn't return what I was looking for, so I tried chat GPT which offered the solution below, but the form is still submitted and the options overwritten with blank values.
// Check if the form has been submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Check if the form fields are set and not empty before updating options $ID = isset($_POST['myplugin_id']) ? sanitize_text_field($_POST['myplugin_id']) : ''; $apiKey = isset($_POST['myplugin_api_key']) ? sanitize_text_field($_POST['myplugin_api_key']) : ''; // Update options only if the corresponding form fields are not empty if (!empty($ID)) { update_option('myplugin_app_id', $ID); } if (!empty($apiKey)) { update_option('myplugin_api_key', $apiKey); } }