Thursday, 26 December 2013 15:48

how to add and get wordpress options in array

Written by 
Rate this item
(0 votes)
In wordpress plugins, you might have saved a lot of
options following way, which in turn creates a lot of
records in wordpress options table!, which does not look
good.
add_option( 'general_settings1', $general_settings1_value );

add_option( 'general_settings2', $general_settings2_value );  
.........more

Instead of this, you may follow the following way by which all
options is saved in one array(general_settings) and only one record
is created.
 
if(!get_option('general_settings')) {
 $general_settings_values = array(
   'general_settings1' => 'wordpress',
   'general_settings2' => ''
   ......more options
                                                                                                        
 );
        
 add_option( 'general_settings', $general_settings_values );
}    

so, how do you get back those options to use in your plugin?
this depicts how -

$options = get_option('general_settings');

then you get the option value as $options['general_settings1']

hope that helps, If you have further questions, post in forum in
this website
Read 1338 times Last modified on Saturday, 28 December 2013 23:35
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments