get_theme_mod()WP 2.1.0

Gets the value of the specified option (setting) of the current theme.

If the specified theme option does not exist, the second parameter $default will be passed through the php sprintf() function before returning as follows:

$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );

Can be used in conjunction with set_theme_mod() to get the settings of other theme options. It will be an alternative to the Settings API, but for the theme.

1 time — 0.00212 sec (very slow) | 50000 times — 0.54 sec (very fast) | PHP 7.4.33, WP 6.1.1
Hooks from the function

Returns

Mixed. The value of the specified theme option.

Usage

get_theme_mod( $name, $default );
$name(string) (required)

The name of the theme setting to retrieve. For example: 'background_color'.

Possible values (additional values are usually added through the customizer):

background_color
background_image_thumb
background_position_x
background_position_y
background_size — auto, contain, cover
background_repeat — repeat-x, repeat-y, repeat, no-repeat
background_attachment
header_image
header_image_data
header_video — ID
header_text
custom_logo — ID
external_header_video — URL
nav_menu_locations — Array
header_textcolor
custom_css_post_id — ID
sidebars_widgets
$default(boolean/string)

A string to return if the specified theme setting could not be found.

The string can contain placeholders. For example

Option not available for %s %s

will return

Option not available for http://example.com/wp-content/themes/theme_name http://example.com/wp-content/themes/theme_name-child

Default: false

Examples

0

#1 Background color from themes settings

This example shows how to add a top border for the footer block with the same color as the background color set in the settings.

You can insert the code at the beginning of header.php

<style>
.footer {
	 border-top: solid 1px #<?= get_theme_mod('background_color') ?>;
}
</style>
0

#2 Background color with a default value

Sometimes you need to set default value for avoid any bad situation. This example could be used to add the custom background color for the .footer block:

.footer {
	 background: #<?= get_theme_mod( 'background_color', '#fff' ) ?>;
}
0

#3 Recommended to escape value with proper escaping function

.search-bar {
	background-color: <?= esc_html( get_theme_mod( 'talash_background_color', '#000' ) ) ?>;
}
<a href="<?= esc_url( get_theme_mod( 'talash_url' ) ); ?>">

Changelog

Since 2.1.0 Introduced.

get_theme_mod() code WP 6.8.3

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 );
}