0

I'm using a class to add a theme option page to my theme.

I need to set some default values upon theme activation or installation, since some of these values are strings, and if i try to get the option using get_option, i won't know if the the user actually left them blank or it's not set at all.

<?php 
    class Theme_Options {
        public function __construct() {
            // We only need to register the admin panel on the back-end
            if ( is_admin() ) {
                add_action( 'admin_menu', array( 'Theme_Options', 'add_admin_menu' ) );
                add_action( 'admin_init', array( 'Theme_Options', 'register_settings' ) );
            }
        }
        // Returns all theme options 
        public static function get_theme_options() {
            return get_option( 'theme_options' );
        }
        // Returns single theme option
        public static function get_theme_option_value( $id ) {
            $options = self::get_theme_options();
            if ( isset( $options[$id] ) ) {
                return $options[$id];
            }
        }
        // Add sub menu page
        public static function add_admin_menu() {
            add_menu_page(
                esc_html__( 'Theme\'s Options', 'sample' ),
                esc_html__( 'Theme\'s Options', 'sample' ),
                'manage_options',
                'theme-settings',
                array( 'Theme_Options', 'create_admin_page' )
            );
        }
        // Register a setting and its sanitization callback.
        public static function register_settings() {
            register_setting( 'theme_options', 'theme_options', array( 'Theme_Options', 'sanitize' ) );
        }
        // Sanitization callback
        public static function sanitize( $options ) {
            // If we have options lets sanitize them
            if ( $options ) {
                // Input
                if ( ! empty( $options['sample_input'] ) ) {
                    $options['sample_input'] = sanitize_text_field( $options['sample_input'] );
                } else {
                    unset( $options['sample_input'] ); // Remove from options if empty
                }
            }
            // Return sanitized options
            return $options;
        }
        // Settings page output
        public static function create_admin_page() { ?>
            <div class="wrap">
                <form method="post" action="options.php">
                    <?php settings_fields( 'theme_options' ); ?>
                    <table class="form-table"><tbody>
                        <tr>
                            <th scope="row">
                                <?php esc_html_e( 'Sample input', 'sample' ); ?>
                            </th>
                            <td>
                                <?php $value = self::get_theme_option_value( 'sample_input' ); ?>
                                <input type="text" name="theme_options[sample_input]" value="<?php echo esc_attr( $value ); ?>"> 
                            </td>
                        </tr>
                    </tbody></table>
                    <?php submit_button(); ?>
                </form>
            </div>
        <?php }
    }
new Theme_Options();
// Helper function to use in theme to return a theme option value
function get_theme_option( $id = '' ) {
    return Theme_Options::get_theme_option_value( $id );
}?>

Note: I removed a lot of unnecessary lines to avoid the question being too long

Since the class is storing serialized data, i thought it's not the best idea to just save a default string to the database.

Is there any way to run this class upon activation and store some default values?

2

Instead of using a hook to store the defaults, you can write the logic straight into get_theme_options - if the value is false, there are no options saved yet:

public static function get_theme_options() {
    $options = get_option( 'theme_options' );

    if ( $options === false ) {
        $options = [
            'key' => 'default_value',
        ];

        add_option( 'theme_options', $options );
    }

    return $options;
}
  • Thanks, i usually forget the most obvious approaches :) BTW sorry for the delay. – Jack Johansson May 15 '17 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.