Save array into WordPress options
-
Hello,
I am having a problem when saving an array into WordPress options. Basically, I have two different settings pages that will be saved into one database row as an array. These two different pages is registered using one
register_setting()and two separateadd_settings_section().In my sanitizing function, all data are saved correctly except one
selectelement which accept multiple selections. It will be saved as multidimensional array in the one database row. Here is my code to save that array.public function sanitize_options( $input ) { $options = get_option( 'prfx_options' ); if ( isset( $input['post_types'] ) && count( $input['post_types'] ) > 0 ) { $input['post_types'] = array_map( 'esc_attr', $input['post_types'] ); } elseif { $input['post_types'] = array(); } if ( is_array( $wpsdcp_options ) ) { return array_merge( $options, apply_filters( 'prfx_options_sanitization', $input ) ); } else { return apply_filters( 'prfx_options_sanitization', $input ); } }It should accept 0 or more selections. It works fine when there’s at least one selection that is selected. But when there’s nothing that is selected, it won’t save it. I’ve tried
count()andempty()but neither would work. Is there something wrong with my code?Thank you in advance!
Mauris
The topic ‘Save array into WordPress options’ is closed to new replies.