So I originally suggested normalizing the options array; i.e. merging saved values with the default values. And this does solve the “undefined index” notice:
$my_options = wp_parse_args( get_option( 'my_options' ), [
'my_background_color_option' => '#000',
// ... other args.
] );
But to make sure that an option is not empty (e.g. not ''
, false
, null
, 0
or []
), you can use empty()
like so: (credits to this answer and yourself/OP)
<div id="identifier" class="classname" style="background-color: <?php echo esc_attr( // wrapped for clarity
( ! empty( $my_options['my_background_color_option'] ) ) ?
$my_options['my_background_color_option'] : '#000'
); ?>">
There’s also the array_merge()
, but the above is indeed something that is good for validating an option. ( But sorry, I was focused on the “undefined index” thing and kinda forgot the (avoiding a) false return
… =) )
Related Posts:
- Upgrade 4.5.2 to 4.5.3 deleting row in wp_options table
- How to add field for new site-wide option on Network Settings screen?
- Modifying a JS file with data from plugin settings
- Correct way to serialize the data of options table?
- Where should I use get_option in a plugin
- Is there a hook attached to general settings save?
- How should I use register_setting, add_settings_section, & add_settings_field in my plugin’s options page?
- Custom plugin settings: clicking “save changes” does not display success message
- Duplicate settings of master blog to slave blogs in WP multi-site installation
- Environment Specific Options Token
- How do I extract the Alt Text from an image attachment upload via the WordPress Options Framework Plugin?
- How to fetch serialized data from wordpress options
- Changing widget options via the functions.php when there are no hooks
- add function to saving change on Options Pages
- WooCommerce “Save Changes” button appears inside custom settings
- clean wp_options table unused rows
- How to set CORS header?
- Prepare plugin options for multi-lingual support
- Remove obsolete plugins artifacts from database tables
- Creating a plugin that will add options. Where should the options menu pages go?
- Add get_option to jquery
- update_user_meta add value on the top on existing value
- Can’t get plugin settings page to save data
- How do i save plugin-specific info?
- Set the active Navigation Menu from a plugin
- problems with wordpress and php version 5.3.3-1
- Where does WordPress store plugin (enabled/disabled) status for multisite?
- Create mobile navigation header with settings integration
- Admin Theme customization
- How to get specific setting by settings_fields()?
- Add Plugin options as subpage to Theme options page
- changes to widget settings not showing up in individual pages pages until they’re updated [closed]
- Show admin notice on incorrect value on form field
- Plugin options page with live preview?
- WordPress plugin admin html being shown in Customizer iframe
- wp_editor content in plugin options page
- remove add new post, if there is already one post
- How to create an option page for this simple plugin
- Redirect to another page using contact form 7? [closed]
- Gathering and logging data from a plugin: how to do it without race conditions?
- Update a core option from plugin settings page
- Can I use register_settings and unregister_setting once the settings page has loaded?
- update_option_{$option} not showing old value
- Updating options into the wp database performance
- Can deactivating a plugin sometimes “reset” settings within that plugin?
- Change the style of h1, h2, only in post entry
- How to add options to the plugin page?
- How to prevent plugins from sniffing/stealing other plugins’ options?
- Default plugin config to override wp_options?
- Update WP option by plugin
- How to customize this theme? [closed]
- How (or where) do I get wordpress plugin update download link?
- Adding option values as an array using a multi selectable select box
- Overwrite add_image_size defined by theme
- WordPress API functions not working at AJAX functions.php call
- Every time I save my plugin options it erases another plugin options
- Changing plugin options from theme functions file?
- Need to add/remove group of options and display them as rows
- Multisite Plugin – Access options (wp_options) on child sites
- Using call_user_func() within add_settings_section() within a Class
- What is the most efficient way of adding additional functionaliy for admin only
- How to save Setting pages data to the database?
- Elementor Sidebar not loading when PRO elements plugin is active
- Save log for imports in WordPress database – options-table, custom post type or other place?
- Creating an array from form inputs before it is posted to the options database
- dokan edit functionality
- Load google font script based on optgroup label selected
- when i activate my WordPress plugin cannot see customizer options or preview
- save short-code in my custom plugin settings problem missed something
- WordPress appearance button
- Plugin Options Page and Subpage
- save multi-select input using update_option for plugin
- How to save the option’s new values plus old value without overwrite old one
- how to add navigation icons in specific template?
- Admin Custom Login loses settings on Code and Data migration
- Customizer preview URL not updating when settings reset to default
- Echo custom admin field into a is_single()
- Protect WordPress option value
- WordPress Boilerplate Plugin doesn’t see callback functions for add_settings_field and add_settings_section
- How to detect if i am on plugin’s settings page
- How to store multiple instances of the plugin’s options for various shortcodes
- Manual Backup – How to save to Firefox or Chrome (instead of IE which brings up popup I can’t get rid of)
- Register setting on plugin activation
- How to add separator between default and custom panels in WordPress customizer?
- Plugin option for input box validation
- Options page textarea to populate custom post select field
- How to create a new database table whenever user changes options
- get_option return incorrect values
- Add a checkbox in plugin options page and make it actually work
- Create Array from data in the OPTIONS table
- Is saving multiple options or saving multiple items in one option better?
- Error when renaming option
- disable lightbox in a plugin [closed]
- ‘all’ hook and get_option issue
- How to find an option in the database?
- Building a simple “settings” plugin to change textstrings on the home page
- How to programmatically detect how many posts are displayed per page?
- selecting options from another form
- Issue with customizer and widget page
- wp_options Table keeps growing