Using WordPress ‘get_theme_mod()’ PHP function

The get_theme_mod() WordPress PHP function retrieves theme modification value for the active theme.

Usage

To use the function, simply call it like this: get_theme_mod('modification_name', 'default_value')

Parameters

  • $name (string) – Required. Theme modification name.
  • $default_value (mixed) – Optional. Theme modification default value. Default: false.

More information

See WordPress Developer Resources: get_theme_mod

Examples

This example retrieves the custom background color and applies it as a border on the top of the footer.

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

Get custom background color with a default value

This example retrieves the custom background color with a default value of white and applies it as a border on the top of the footer.

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

Properly escape the output of get_theme_mod

This example demonstrates how to use the esc_html() function to properly escape the output of get_theme_mod() when setting a background color.

.search-bar {
  background-color: <?php echo esc_html(get_theme_mod('talash_background_color', '#000')); ?>;
}

Escape the output of get_theme_mod for a URL

This example demonstrates how to use the esc_url() function to properly escape the output of get_theme_mod() when setting a URL.

<a href="<?php echo esc_url(get_theme_mod('talash_url')); ?>">

Get custom logo URL

This example retrieves the custom logo URL and sets it as the src attribute of an img element.

<img src="<?php echo esc_url(get_theme_mod('custom_logo')); ?>" alt="Logo">