Wordpress storing theme options Wordpress storing theme options wordpress wordpress

Wordpress storing theme options


Yes, if you do a little of processing (read: unserialize) after having readed the configuration from DB!

Depending on the number of options you'll save query to the DB.


Serialization is not necessary. There are built-in methods for storing theme options neatly in an array.

Here is a complete example including all of the necessary code:

First, in your theme's functions.php file, you have to register the settings you will be using by writing a little function and using the WordPress hook to activate it:

<?php    function my_theme_admin_init() {        register_setting('my_options', 'my_theme_options');    }    add_action('admin_init', 'my_theme_admin_init');?>

Then, in the place where you want to use the options, use this bit of html and php. (Notice that the form posts to options.php. Doing it this way takes advantage of the WordPress built-in functionality that handles saving the options for you):

<form method="post" action="options.php">    <?php     // Load the options from the WP db    $options = get_option('my_theme_options');    // WP built-in function to display the appropriate fields for saving options    settings_fields("my_options"); ?>    <table class="form-table">        <tr>            <th scope="row">Facebook URL:</th>            <td>                <input type="text" name="my_theme_options[facebook]" size="40" value="<?php echo stripslashes($options["facebook"]); ?>" />            </td>        </tr>        <tr>            <th scope="row">Twitter URL:</th>            <td>                <input type="text" name="my_theme_options[twitter]" size="40" value="<?php echo stripslashes($options["twitter"]); ?>" />            </td>        </tr>        <tr>            <th scope="row">LinkedIn URL:</th>            <td>                <input type="text" name="my_theme_options[linkedin]" size="40" value="<?php echo stripslashes($options["linkedin"]); ?>" />            </td>        </tr>    </table>    <p class="submit">        <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />    </p></form>