get_theme_mod() or get_option(): where to store settings
WordPress has two places for settings. Theme mods (get_theme_mod(), set_theme_mod()) belong to the active theme and are stored together in one option per theme. Options (get_option(), update_option()) belong to the site and stay when the theme changes. Choosing the wrong one means settings that disappear after a theme switch, or theme colours that survive into a theme where they make no sense.
Problem: A theme has a header colour and a footer text in the Customizer, a plugin has an API key and feature switches, and the developer is not sure which storage to use for which.
Solution: Use theme mods for settings that describe the look of this theme (colours, layout choices, footer text). Use options for everything that belongs to the site or a plugin (API keys, feature switches, business data), registered with register_setting() and their defaults.
Theme mods
<?php $header_color = get_theme_mod( 'ha_header_color', '#1e40af' ); // the second argument is the default $footer_text = get_theme_mod( 'ha_footer_text', get_bloginfo( 'name' ) ); set_theme_mod( 'ha_header_color', '#0a2342' ); remove_theme_mod( 'ha_header_color' ); $all = get_theme_mods(); // all mods of the active theme
All mods of a theme are stored in one option, theme_mods_{theme-slug}. A child theme has its own set, so switching from the parent to a child theme starts with empty mods. In the Customizer they are created with $wp_customize->add_setting() (type theme_mod is the default), as shown in instant Customizer previews with postMessage.
Options
<?php
add_action( 'admin_init', function () {
register_setting( 'ha_settings', 'ha_api_key', array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
) );
register_setting( 'ha_settings', 'ha_features', array(
'type' => 'array',
'sanitize_callback' => function ( $value ) {
return array( 'analytics' => ! empty( $value['analytics'] ), 'chat' => ! empty( $value['chat'] ) );
},
'default' => array( 'analytics' => true, 'chat' => false ),
) );
} );
$key = get_option( 'ha_api_key' );
$features = get_option( 'ha_features' );
The default in register_setting() (WordPress 4.7+) is returned by get_option() when the option does not exist; register the setting on init instead of admin_init if the front end reads it too, otherwise the default is known only in the admin.
A Customizer setting stored as an option
A plugin can show its settings in the Customizer and still keep them as options:
<?php
$wp_customize->add_setting( 'ha_banner_text', array(
'type' => 'option', // stored with update_option(), survives theme changes
'capability' => 'manage_options',
'sanitize_callback' => 'sanitize_text_field',
) );
The rule of thumb
Ask “should this setting survive a theme switch?”. Header colour, sidebar position, logo layout: no, theme mod. Contact email, API key, opening hours, cookie banner text: yes, option. Data like the opening hours is also something a new theme should be able to show, which is easier when it is a site option and not hidden in the old theme’s mods.
NOTE: Every get_theme_mod() call reads the same cached option, so many calls are cheap. Options are cached too when they are autoloaded (the default); large options read only on one admin screen should be created with autoload off.