WordPress storing theme options

Mar 13, 2012   //   by phpfreelancer.biz   //   Blog, Wordpress Theme Tutorial  //  No Comments

I am currently storing all of my theme options individually using the update_option(name, value); function, but I am thinking of putting them all into an array, serializing it, and storing a single option in the database.

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');
?>

advantage of the WordPress built-in functionality that handles saving the options

<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>

Leave a comment

Share This Post

RSS Wordpress News

  • The WordPress.com Year in Review (and Resolutions for 2018) January 3, 2018
    It was quite a year for the WordPress.com community, and we've got a lot to look forward to.
  • Managing Your Blog On a Mobile Device December 13, 2017
    A few enhancements we've added to the iOS and Android apps for easier blogging.
  • New Premium Themes: Small Business and Photo Blog December 7, 2017
    Introducing two new premium themes: Small Business, made for your entrepreneurial endeavors, and Photo Blog, designed to make your photography shine.
  • Updated Privacy Policy December 4, 2017
    As part of our commitment to privacy and transparency, we’re updating our Privacy Policy. We want to give you more information about how we collect and use personal information — in a more organized and readable format. “Your privacy is critically important to us.” These are the first words of …
  • Update Your Avatar on WordPress.com October 12, 2017
    Manage, upload, and edit your profile photo at wordpress.com/me.
  • A New Media Picker for the iOS WordPress App October 9, 2017
    Sometimes it’s the extra touches that make all the difference; on your website, that’s the photos and video that give your content life. You asked for streamlined access to your media library and the camera and photos on your device, to make uploading and inserting photos and videos easier. We …