WordPress provides a default method, wp_parse_args
, to combine a set of default options and a user-defined set of options. This method uses PHP’s native array_merge
for arrays, but also works when either of the two option sets is an object or a WordPress options string.
You simply provide an array of default options and an array of set options, and WordPress fills the options that are missing in the options set by their default value.
You can find this code, adjusted for your example, below:
$options = get_option( 'plugin_options' );
$options_default = array(
'plugin_option_1' => '',
'plugin_option_2' => '',
'plugin_option_3' => '',
'plugin_option_4' => '',
'plugin_option_5' => ''
);
$options = wp_parse_args( $options, $options_default );
Related Posts:
- Multiple entries in get_option results? or why is _multiwidget set to 1?
- array_map() for sanitizing $_POST
- Problem with display data from get_option
- Decontruct serialized data array from wp_options
- framework for plugin/theme options panel? [closed]
- How do I add CSS options to my plugin without using inline styles?
- Is get_option function cached?
- How come `wp_options` table does not have an index on `autoload`?
- Update Option Stored in Multi-Dimensional Array
- What are the advantages to the Settings API?
- Adding Widget form fields dynamically
- Where to store plugin settings fields
- What’s the best method for emptying an option created with the Settings API?
- Releasing new plugin version, how to rename old options keys?
- WordPress updates defined vs add_filter?
- Creating Settings Page with dropdowns for Plugin
- How to properly sanitize strings for update_option()
- How to add a new plugin page under desired Options page?
- Formatting of curly brackets array from WP database to get more readable output
- How do I handle multiple Submit buttons in plugin’s option page?
- Deletion of shared options using uninstall.php
- How to fetch serialized data from wordpress options
- Why won’t register_setting() create a setting?
- Dropdown list of pages to get page id to store in plugin options
- update_option is not working!
- update_option_{$option} Too Few Arguments
- Save user-specific options in WordPress admin
- How to store accumulate multiple option values in a single array using Options_API
- Creating option to allow user to select the page my plugin content will display on
- Unified Approach for Placing Option Pages
- WordPress plugin options and jQuery
- WordPress Plugin Setting’s POST
- Passing array of strings to a SQL statement in a WordPress plugin
- Performance of several get_option() calls
- Can I use the different settings sections over different pages using the save options group?
- Options API – Validation Callback $input is NULL
- Best choice of options/settings framework for plugin/theme development
- Correct way check nonce (security) using old Options API
- admin_notices not displaying in plugin
- How to store a medium amount of options?
- How to add settings subpage from a plugin to a settings page created in theme?
- Plugin options not being saved or created
- Remove Internal Style Sheet if no Value Provided?
- What could cause my plugin’s options/settings page not to load?
- How to set “Site Address (URL)” programmatically on WP multisite?
- WordPress setting with select – where is my mistake?
- Using wp_parse_args to set up Plugin Default Settings
- Custom delete option button in plugin settings
- Storing product price data in the database
- How To Change Post Author Default?
- WP_Editor – Saving Value into Plugin Option – Stripping HTML
- Editing options pages?
- Multiple options pages validation for a plugin
- Notice: Trying to get property ‘term_id’ of non-object
- get_option() not returning expected value from plugin
- Parse form values before sending to options.php
- Prevent invalid or empty values from being saved to the database and retain the form field values upon error
- Show error message after exception handled
- Insert/Update values to a table after blogname option etc. is updated
- Access bloginfo, get_option, and plugins_url from a non-core php file
- wordpress how to query wp_options table
- How Can I add Fields in wp-option table?
- Redirect to another page using contact form 7? [closed]
- Checked() function on a multidimensional array
- get_option() will not work without access to wp-config.php
- Show message from backend
- Displaying “One Time” Notification in Plugins
- Issue on Checkbox with Custom Option Page
- The Correct Way to Use Nonce Field without Settings API
- Settings API: Setting default option via ‘get_option’ fails
- how to save wp_editor html content in options table
- Custom Plugin Options Won’t Update
- Working of foreach loop with array
- Use options to control jQuery plugin
- why does get_option( ‘home’ ) not work while using in plugin [closed]
- Plugin Options not saving options and doesn’t work
- Add multiple checkboxes as single field to Custom Shipping Method Settings Page
- How does one update complex options?
- How to get the `comment_post_ID`?
- update_option() passing empty array() but still updating
- Plugins Settings page not updating
- How to create plugin settings page for each admin user?
- Plugin setting page – update_option problem
- add_settings_error on validating plugin options API
- Convert each new line in the textfield as a new value in an array
- Array/List Edit in Backend
- What should I use instead of get_blog_option?
- Tie specific functions to options-update for limiting API requests
- When using an options array the Settings API isn’t creating the database record
- Can’t switch theme after activation
- How do I build a settings panel under the plugin
- update_option() updating the option with an empty value?
- the correct way to use options from settings page [closed]
- Add_menu_page and saving settings
- Plugin options page – save two related options as one entry
- Saving multiple fields as array
- Options don’t save, validation callback not executed
- Modify a Themes Appearance >> Header admin screen from a plugin
- Settings API not Saving to Database or Display
- Admin Message after Plugin Option Updated