wppaste
WordPress

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.

Retrieves theme modification value for the active theme.

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?

Theme mods are stored per theme. get_theme_mod() calls get_theme_mods(), which reads the theme_mods_{stylesheet} option keyed to the currently active stylesheet, so a value set under one theme is invisible once a different theme is active.

Why did my default string get mangled with a theme URL I never asked for?

When $default_value is a string containing an sprintf-style %s pattern, the function runs it through sprintf() with get_template_directory_uri() and get_stylesheet_directory_uri(). A default that happens to contain a stray percent sign followed by 's' gets rewritten unexpectedly.

Why is a plugin changing the value I get back even though I never touched the option?

Both the saved-value path and the default-value path run the result through the theme_mod_{$name} filter before returning it, so any plugin or theme hooking that dynamic filter name can alter what get_theme_mod() reports regardless of what is actually stored.

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–30

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 38.

Plugin surface
1 hook

Third-party callbacks on 'theme_mod_{$name}' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
always15–17get_theme_mods()apply_filters()
!isset($mods[$name]) && is_string($default_value) && $default_value30get_theme_mods()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.44415–363More instructions than PHP 8.4
8.14415–363More instructions than PHP 8.4
8.24415–363More instructions than PHP 8.4
8.34415–363More instructions than PHP 8.4
8.43815–303Compiles 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:

  1. apply_filters( theme_mod_{$name} )filterline 1082 (+15 into the body)

    Filters the theme modification, or 'theme_mod', value.

  2. apply_filters( theme_mod_{$name} )filterline 1095 (+28 into the body)

    Filters the theme modification, or 'theme_mod', value.

Uses · 4

Used by · 50

Show all 50

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.