get_theme_mod() WordPress Function

The get_theme_mod() function allows you to retrieve a theme modification setting for the current theme. This setting can be anything defined by the theme, such as the color scheme or the background image.

get_theme_mod( string $name, mixed $default = false ) #

Retrieves theme modification value for the active theme.


Description

If the modification name does not exist and $default is a string, then the default will be passed through the sprintf() PHP function with the template directory URI as the first value and the stylesheet directory URI as the second value.


Top ↑

Parameters

$name

(string)(Required)Theme modification name.

$default

(mixed)(Optional) Theme modification default value.

Default value: false


Top ↑

Return

(mixed) Theme modification value.


Top ↑

Source

File: wp-includes/theme.php

function get_theme_mod( $name, $default = 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 ) ) {
		// Only run the replacement if an sprintf() string format pattern was found.
		if ( preg_match( '#(?<!%)%(?:\d+\$?)?s#', $default ) ) {
			// Remove a single trailing percent sign.
			$default = preg_replace( '#(?<!%)%$#', '', $default );
			$default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
		}
	}

	/** This filter is documented in wp-includes/theme.php */
	return apply_filters( "theme_mod_{$name}", $default );
}


Top ↑

Changelog

Changelog
VersionDescription
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More