Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 81
Loading an Array of Options
ОглавлениеYou have seen that saving multiple options in a single array is best practice. A complete example of saving and then getting values from one array would be as follows:
<?php // To store all of them in a single function call: $options = array( 'color' => 'red', 'fontsize' => '120%', 'border' => '2px solid red' ); update_option( 'pdev_plugin_options', $options ); // Now to fetch individual values from one single call: $options = get_option( 'pdev_plugin_options' ); // Store individual option values in variables $color = $options[ 'color' ]; $fontsize = $options[ 'fontsize' ]; $border = $options[ 'border' ]; ?>
Saving and retrieving options enclosed in an array has another advantage: variable Boolean types within the array are preserved. Consider the following example:
<?php add_option( 'pdev_test_bool', array( 'booltrue' => true, 'boolfalse' => false ) ); ?>
Now get the option value from the database with var_dump( get_option( 'test_bool' ) )
. See how Boolean types are retained, contrary to the previous example:
// output result var_dump( get_option( 'pdev_test_bool' ) ); array(2) { ["booltrue"] => bool(true) ["boolfalse"]=> bool(false) }