get_theme_mod( string $name, bool|string $default = false )

Retrieve theme modification value for the current theme.


Description Description

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


Parameters Parameters

$name

(string) (Required) Theme modification name.

$default

(bool|string) (Optional)

Default value: false


Top ↑

Return Return

(mixed)


Top ↑

Source 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 string $current_mod The value of the current theme modification.
		 */
		return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
	}

	if ( is_string( $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

Changelog
Version Description
2.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content
    Contributed by bdthemes

    Calling 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 as a border on the top of the footer with default color white. It would be css inserted in the header:

    .footer {
         border-top: solid 1px #<?php echo get_theme_mod( 'background_color', '#fff' ); ?>;
    }

You must log in before being able to contribute a note or feedback.