get_theme_mod( string $name, mixed $default_value = false ): mixed
- Since
- 2.1.0
- Source
wp-includes/theme.php:1067
Fetches a single theme modification value for the active theme's stylesheet, falling back to $default_value when the setting has not been saved. It reads from the theme_mods_{stylesheet} option via get_theme_mods(), so values are lost if the active theme changes. Pair it with set_theme_mod() to write the value and remove_theme_mod() to delete it.
Description
If the modification name does not exist and $default_value is a string, then the default will be passed through the https://www.php.net/sprintf sprintf() PHP function with the template directory URI as the first value and the stylesheet directory URI as the second value.
Parameters
$namestring- Theme modification name.
$default_valuemixedoptional- Theme modification default value. Default false.Default:
false
Return
mixed- Theme modification value.
Examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get a saved theme mod with a fallback color
A theme or plugin stores an accent color as a theme mod and needs to read it back with a sane default.
$accent_color = get_theme_mod( 'example_accent_color', '#000000' );
printf( 'Accent color: %s', esc_html( $accent_color ) );Build a default asset URL relative to the theme directory
A watermark image path isn't set yet, so the default should point into the active theme's own folder.
$watermark_url = get_theme_mod( 'example_watermark_image', '%s/assets/watermark.png' );
printf( 'Watermark URL: %s', esc_html( $watermark_url ) );Because the default contains a %s placeholder, it is run through sprintf() with the template directory URI, so the printed path points into the current theme's folder.
Common problems · 3
- Why does get_theme_mod return my old value after I switched themes?
- Why did my default string get mangled with a theme URL I never asked for?
- Why is a plugin changing the value I get back even though I never touched the option?
Why does get_theme_mod return my old value after I switched themes?
Why did my default string get mangled with a theme URL I never asked for?
Why is a plugin changing the value I get back even though I never touched the option?
When to use something else
set_theme_mod- When you need to save or update the modification value rather than read it.
get_theme_mods- When you need every stored modification for the active theme at once instead of a single key.
remove_theme_mod- When you need to delete a single stored modification so it falls back to its default.
get_option- When the setting is a global site option rather than something scoped to the active theme's stylesheet.
Cost profile
Measured from the compiled opcodes, not from a stopwatch. Every number here is identical on any machine that runs the same PHP version, and every function in core is ranked by these figures.
- Cost class
- Moderate
- Scaling
- Constant
- Instructions
- 15–30
- Plugin surface
- 1 hook
- Called by
- 50
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 38.
Third-party callbacks on 'theme_mod_{$name}' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()one call below get_theme_mod()
Further down the call graph this can also reach cache, serialize, transient and query — those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_theme_mod() can have, taken from its control-flow graph on PHP 8.4.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–17 | get_theme_mods()apply_filters() |
!isset($mods[$name]) && is_string($default_value) && $default_value | 30 | get_theme_mods()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
7.4 | 44 | 15–36 | 3 | More instructions than PHP 8.4 |
8.1 | 44 | 15–36 | 3 | More instructions than PHP 8.4 |
8.2 | 44 | 15–36 | 3 | More instructions than PHP 8.4 |
8.3 | 44 | 15–36 | 3 | More instructions than PHP 8.4 |
8.4 | 38 | 15–30 | 3 | Compiles to the same instructions |
Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.
Hooks fired · 2
2 hooks fire while get_theme_mod() runs, in this order:
- apply_filters( theme_mod_{$name} )filterline 1082 (+15 into the body)
Filters the theme modification, or 'theme_mod', value.
- apply_filters( theme_mod_{$name} )filterline 1095 (+28 into the body)
Filters the theme modification, or 'theme_mod', value.
Uses · 4
- get_theme_mods()Retrieves all theme modifications.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_template_directory_uri()Retrieves template directory URI for the active theme.
- get_stylesheet_directory_uri()Retrieves stylesheet directory URI for the active theme.
Used by · 50
- Custom_Background::admin_page()Displays the custom background page.
- Custom_Image_Header::show_header_selector()Displays UI for selecting one of several default headers.
- Twenty_Twenty_One_Custom_Colors::body_class()Adds a class to if the background-color is dark.
- Twenty_Twenty_One_Custom_Colors::custom_color_variables()Customizer & frontend custom color variables.
- Twenty_Twenty_One_Custom_Colors::editor_custom_color_variables()Editor custom color variables.
- Twenty_Twenty_One_Custom_Colors::generate_custom_color_variables()Generates color variables.
- Twenty_Twenty_One_Dark_Mode::admin_body_classes()Adds a class to the element in the editor to accommodate dark-mode.
- Twenty_Twenty_One_Dark_Mode::customizer_controls()Registers customizer options.
- Twenty_Twenty_One_Dark_Mode::editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- Twenty_Twenty_One_Dark_Mode::html_classes()Calculates classes for the main element.
- Twenty_Twenty_One_Dark_Mode::switch_should_render()Determines if we want to print the dark-mode switch or not.
- WP_Customize_Setting::get_root_value()Get the root value for a setting, especially for multidimensional ones.
Show all 50
- WP_REST_Server::add_site_logo_to_index()Exposes the site logo through the WordPress REST API.
- _custom_background_cb()Default custom background callback.
- _custom_logo_header_styles()Adds CSS to hide header text for custom logo, based on Customizer setting.
- _delete_attachment_theme_mod()Checks an attachment being deleted to see if it's a header or background image.
- _get_random_header_data()Gets random header image data from registered images in theme.
- _wp_block_theme_register_classic_sidebars()Registers the previous theme's sidebars for the block themes.
- display_header_text()Whether to display the header text.
- get_background_color()Retrieves value for custom background color.
- get_background_image()Retrieves background image for custom background.
- get_custom_header()Gets the header image data.
- get_custom_logo()Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
- get_header_image()Retrieves header image for custom header.
- get_header_textcolor()Retrieves the custom header text color in 3- or 6-digit hexadecimal form.
- get_header_video_url()Retrieves header video URL for custom header.
- get_media_states()Retrieves an array of media states from an attachment.
- get_nav_menu_locations()Retrieves all registered navigation menu locations and the menus assigned to them.
- has_custom_logo()Determines whether the site has a custom logo.
- is_random_header_image()Checks if random header image is in use.
- retrieve_widgets()Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar, and saves the widget settings. This has to run at least on each theme change.
- switch_theme()Switches the theme.
- twenty_twenty_one_setup()Sets up theme defaults and registers support for various WordPress features.
- twentyfifteen_color_scheme_css()Enqueues front-end CSS for color scheme.
- twentyfifteen_get_color_scheme()Gets the current Twenty Fifteen color scheme.
- twentyfifteen_header_background_color_css()Enqueues front-end CSS for the header background color.
- twentyfifteen_sidebar_text_color_css()Enqueues front-end CSS for the sidebar text color.
- twentyfourteen_body_classes()Extends the default WordPress body classes.
- twentyfourteen_scripts()Enqueues scripts and styles for the front end.
- twentynineteen_colors_css_wrap()Displays custom color CSS in customizer and on frontend.
- twentynineteen_custom_colors_css()Generates the CSS for the current primary color.
- twentynineteen_editor_customizer_styles()Enqueues supplemental block editor styles.
- twentynineteen_image_filters_enabled()Returns true if image filters are enabled on the theme options.
- twentynineteen_setup()Sets up theme defaults and registers support for various WordPress features.
- twentyseventeen_body_classes()Adds custom classes to the array of body classes.
- twentyseventeen_colors_css_wrap()Displays custom color CSS.
- twentyseventeen_content_image_sizes_attr()Adds custom image sizes attribute to enhance responsive image functionality for content images.
- twentyseventeen_content_width()Sets the content width in pixels, based on the theme's design and stylesheet.
- twentyseventeen_custom_colors_css()Generates the CSS for the current custom color scheme.
- twentyseventeen_front_page_section()Displays a front page section.
Source
function get_theme_mod( $name, $default_value = false ) { $mods = get_theme_mods(); if ( isset( $mods[ $name ] ) ) { /** * Filters the theme modification, or 'theme_mod', value. * * The dynamic portion of the hook name, `$name`, refers to the key name * of the modification array. For example, 'header_textcolor', 'header_image', * and so on depending on the theme options. * * @since 2.2.0 * * @param mixed $current_mod The value of the active theme modification. */ return apply_filters( "theme_mod_{$name}", $mods[ $name ] ); } if ( is_string( $default_value ) ) { // Only run the replacement if an sprintf() string format pattern was found. if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default_value ) ) { // Remove a single trailing percent sign. $default_value = preg_replace( '#(?<!%)%$#', '', $default_value ); $default_value = sprintf( $default_value, get_template_directory_uri(), get_stylesheet_directory_uri() ); } } /** This filter is documented in wp-includes/theme.php */ return apply_filters( "theme_mod_{$name}", $default_value );}History
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.